2013-07-18 35 views
2

我有一个包含事件列表的表格。我想基于唯一值“事件”将多行中的单位值连接到一列中以用于显示目的。将不同行中的值合并到一个图层中

当前数据:

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

我想怎么显示出来:

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

我使用的MySQL/PHP。

谢谢!

+2

['GROUP_CONCAT'](http://dev.mysql.com/doc/refman/5.0/en/group- by-functions.html#function_group-concat) – Wrikken

+0

这里是一个很好的投资:http://stackoverflow.com/questions/276927/can-i-concatenate-multiple-mysql-rows-into-one-field?rq=1 ,也是http://stackoverflow.com/questions/12635714/php-mysql-joining-three-tables-and-merging-results?lq=1(这是一种情况下,我嫉妒我的愉快的SQL Server: - /) – user2246674

回答

3

尝试此查询:

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

使用此查询

SELECT date, time, incident, GROUP_CONCAT(unit SEPARATOR ", ") AS unit FROM table GROUP_BY incident 
相关问题