Bayu Anggara
Bayu Anggara

Reputation: 287

Display same ids into single id

I have selected data from database and here is the result:

+--------+------------+
|  room  |    name    |
+--------+------------+
|   12   | John       |
|   13   | Marry      |
|   14   | Camilla    |
|   14   | Beatrix    |
+--------+------------+

And how can I print to my page by php code like this :

12 : John
13 : Marry
14 : Camilla, Beatrix

thanks for helping :)

Upvotes: 0

Views: 51

Answers (2)

Arun Palanisamy
Arun Palanisamy

Reputation: 5459

You may want to use Concat and Group_concat to meet your exact need

select
concat(`room`, ' : ',group_concat(`name` separator ',')) as `Name`
FROM test
group by room;

Check Demo Fiddle Here

Upvotes: 1

Bernd Buffen
Bernd Buffen

Reputation: 15057

try this, group and group_concat is what you search

select room,
GROUP_CONCAT(name)
FROM tablename
group by room;

Upvotes: 3

Related Questions