Jump to content

concatenate entire field


ToonMariner

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. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.