Robert
Robert

Reputation: 43

Join values in different rows into one cloumn

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!

Upvotes: 1

Views: 106

Answers (2)

user1646111
user1646111

Reputation:

Try this query:

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

Upvotes: 3

Oguzhan Ozel
Oguzhan Ozel

Reputation: 1164

Use this query

SELECT date, time, incident, GROUP_CONCAT(unit SEPARATOR ", ") AS unit FROM table GROUP_BY incident

Upvotes: 0

Related Questions