brob Posted February 6, 2007 Share Posted February 6, 2007 Hi can someone help. I need to pull this info from a database and merge it based on the secondary_id can anyone suggest how? id secondary_id name desc ------------------------------- 1 27 toy sega 2 27 toy nintendo 3 32 cd coldplay 4 32 cd nelly Quote Link to comment https://forums.phpfreaks.com/topic/37292-solved-merging-data/ Share on other sites More sharing options...
hvle Posted February 6, 2007 Share Posted February 6, 2007 merge with what brob? Quote Link to comment https://forums.phpfreaks.com/topic/37292-solved-merging-data/#findComment-178235 Share on other sites More sharing options...
brob Posted February 6, 2007 Author Share Posted February 6, 2007 merge with one another. So both sets of seondary_id = 27 are in one statement and both sets of secondary_id = 32 are in another statement. Sorry for being a vague. Quote Link to comment https://forums.phpfreaks.com/topic/37292-solved-merging-data/#findComment-178238 Share on other sites More sharing options...
hvle Posted February 6, 2007 Share Posted February 6, 2007 select * from table1 join table2 on table1.secondary_id=table2.secondary_id; Quote Link to comment https://forums.phpfreaks.com/topic/37292-solved-merging-data/#findComment-178239 Share on other sites More sharing options...
brob Posted February 6, 2007 Author Share Posted February 6, 2007 they are both in the same table please do you know of another way? Quote Link to comment https://forums.phpfreaks.com/topic/37292-solved-merging-data/#findComment-178242 Share on other sites More sharing options...
hvle Posted February 6, 2007 Share Posted February 6, 2007 sorry for my misreading. use grouping: select * from table group by secondary_id order by secondary_id; Quote Link to comment https://forums.phpfreaks.com/topic/37292-solved-merging-data/#findComment-178245 Share on other sites More sharing options...
brob Posted February 6, 2007 Author Share Posted February 6, 2007 hi thanks for this but grouping would remove one of the entries and not maintain the both sets of results i.e. 27 toy toy sega nintendo this is what I want to achieve and not lose any of the data Quote Link to comment https://forums.phpfreaks.com/topic/37292-solved-merging-data/#findComment-178247 Share on other sites More sharing options...
hvle Posted February 6, 2007 Share Posted February 6, 2007 why don't you give visual example of what the results should look like? Quote Link to comment https://forums.phpfreaks.com/topic/37292-solved-merging-data/#findComment-178249 Share on other sites More sharing options...
effigy Posted February 6, 2007 Share Posted February 6, 2007 SELECT name, GROUP_CONCAT(`desc` SEPARATOR ', ') AS items FROM table GROUP BY secondary_id; yields: +------+-----------------+ | name | items | +------+-----------------+ | toy | sega, nintendo | | cd | coldplay, nelly | +------+-----------------+ Quote Link to comment https://forums.phpfreaks.com/topic/37292-solved-merging-data/#findComment-178250 Share on other sites More sharing options...
brob Posted February 6, 2007 Author Share Posted February 6, 2007 i would like to have pulled from the database $id = 27 $type = toy $brand = sega, nintendo $id = 32 $type = cd $brand = coldplay, nelly Quote Link to comment https://forums.phpfreaks.com/topic/37292-solved-merging-data/#findComment-178251 Share on other sites More sharing options...
brob Posted February 6, 2007 Author Share Posted February 6, 2007 effigy thank you so mutch this is exactly what I needed and has worked a treat. Great post Quote Link to comment https://forums.phpfreaks.com/topic/37292-solved-merging-data/#findComment-178256 Share on other sites More sharing options...
fenway Posted February 7, 2007 Share Posted February 7, 2007 Just remember there there's a max_length to group_concat. Quote Link to comment https://forums.phpfreaks.com/topic/37292-solved-merging-data/#findComment-179205 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.