Jump to content

[SOLVED] Combining multiple rows to one row in MySQL

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Metalhead

Metalhead

    Newbie

  • Members
  • PipPip
  • 27 posts
Hi,

I now have something like this;
ID| Name  | Child

--+-------+-------

0 | name0 | sub0a

0 | name0 | sub0b

1 | name1 | sub1a

1 | name1 | sub1b

1 | name1 | sub1c

2 | name2 | sub2a

...

But I really need just one row per name, like this:
ID| Name  | Children

--+-------+---------------------

0 | name0 | sub0a, sub0b

1 | name1 | sub1a, sub1b, sub1c

2 | name2 | sub2a, ...

I was hoping to use the concat_ws-function, but it needs his parameters static as far as I can see, and I can't use a query to define what it's supposed to concatenate...

Can anyone guide me in the right way for solving this?

Edited by Metalhead, 22 January 2011 - 03:29 AM.
Solved


#2
Metalhead

Metalhead

    Newbie

  • Members
  • PipPip
  • 27 posts
I found the solution; I need to use 'group_concat'
Something like;
select id, name, group_concat(child)

  from table

  group by id, name





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users