ToonMariner Posted April 20, 2010 Share Posted April 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199130-concatenate-entire-field/ Share on other sites More sharing options...
andrewgauger Posted April 20, 2010 Share Posted April 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199130-concatenate-entire-field/#findComment-1045288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.