Jump to content

Recommended Posts

Hi peeps,

 

I have two tables in a one-to-many relation ship.

 

what I need is to concatenate all the rows of table 2 so a query will return one result.

 

Some thing like:

 

SELECT

`users`.`user_id`,

CONCAT_WS(',',SELECT `country_id` FROM `user_countries` WHERE `user_countries`.`user_id` = `users`.`user_id`) as `countries`

FROM

`users`

 

If user 2 had been to countries 1,6,8 and 9

 

I want a reult of `user_id` => 2 `countries` => '1,6,8,9'

 

ANY help would be VERY MUCH appreciated.

Link to comment
https://forums.phpfreaks.com/topic/199130-concatenate-entire-field/
Share on other sites

SELECT  `user`.`user_id` , GROUP_CONCAT(  `user_countries`.`country_id` 
SEPARATOR  ',' ) AS  `countries` 
FROM  `user` 
JOIN  `user_countries` ON ( user.user_id = user_countries.user_id ) 
GROUP BY  `user`.`user_id` 

 

Tell me what php shows it returning, because mysql results was a BLOB(11)  and I don't know if it shows up correctly in php or if you need to parse the result somehow. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.