If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Child rows concat

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-04-06, 10:35
PolarBear2k PolarBear2k is offline
Registered User
 
Join Date: Jun 2005
Posts: 79
Child rows concat

Hi,

I was wondering if this is possible in MySQL 4.1.

I have 2 tables: Category, Products.
Each product links to a category using a foreign key.

I want to produce a query that will return the Catagory name in one column and all the product names under that category seperated by commas in the second column.

So, if Category1 has 4 products and Category2 has 2 products the output would be:
Code:
Category      | Products
CategoryName1 | ProductName1, ProductName2, ProductName3, ProductName4
CategoryName2 | ProductName5, ProductName6
I tried playing around with CONCAT_WS and a GROUP BY on CategoryID but I think I have to have a subquery to pull out the product records for each category because the inner join method doesn't give me the right result.

Thanks
Reply With Quote
  #2 (permalink)  
Old 08-04-06, 17:19
PolarBear2k PolarBear2k is offline
Registered User
 
Join Date: Jun 2005
Posts: 79
After a day of searching, I've managed to find the solution.
MySQL has a function called GROUP_CONCAT that does just what I want.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On