2

I have a table that has a list of events. I would like to join the unit values from multiple rows into one column for display purposes based on a unique value, "incident".

Current data:

Date Time Incident Unit
1/1  1200  1234    101
1/1  1200  1234    102

How I would like to display it:

Date Time Incident Unit
1/1  1200  1234    101, 102

I am using mysql/php.

Thanks!

Robert
  • 53
  • 1
  • 1
  • 3
  • 2
    [`GROUP_CONCAT`](http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat) – Wrikken Jul 18 '13 at 22:32
  • And here's a good dupe: http://stackoverflow.com/questions/276927/can-i-concatenate-multiple-mysql-rows-into-one-field?rq=1 , also http://stackoverflow.com/questions/12635714/php-mysql-joining-three-tables-and-merging-results?lq=1 (this is one case where I am jealous with my otherwise happy SQL Server :-/) – user2246674 Jul 18 '13 at 22:36

2 Answers2

3

Try this query:

SELECT `date`, `time`, `incident`, group_concat(`unit`) 
from table group by `incident`
0

Use this query

SELECT date, time, incident, GROUP_CONCAT(unit SEPARATOR ", ") AS unit FROM table GROUP_BY incident
Oguzhan Ozel
  • 1,144
  • 11
  • 14