Reputation: 287
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
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
Reputation: 15057
try this, group and group_concat is what you search
select room,
GROUP_CONCAT(name)
FROM tablename
group by room;
Upvotes: 3