Search code examples
mysqlgroup-concat

Mysql Group Concat ... not quite getting the result I need


I have a query that returns the following rows:

parent_id   child_id
    12882       12856
    12882       12857
    12882       12858
    12884       12863
    12884       12864
    12884       12865
    12884       12866

When I add GROUP_CONCAT(child_id) it returns only one row with the first parent_id and ALL child_id's concat'ed.....

What I need is for the query to return where GROUP_CONCAT gives me 1 record per parent ID and concats the children id's for that parent only... Thoughts?

  parent_id   child_id
  12882     12856, 12857, 12858
  12884     12863, 12864, 12865, 12866

Solution

  • Try this with group by clause

    SELECT parent_id, GROUP_CONCAT(child_id)  from tablename group by parent_id