将MySQL行合并在一起

时间:2015-04-10 15:53:39

标签: mysql merge rows

我的桌子有点像这样:

ID  | Date       | Description          | Notes
 1  | 2015-03-25 | Action Completed     | 
 2  | 2015-02-12 | Action Not Completed |
 1  |            |                      | Completed With Ease
 2  |            |                      | Difficulty Performing

我可以使用任何代码通过ID将它们合并在一起吗?

 ID | Date       | Description          | Notes
 1  | 2015-03-25 | Action Completed     | Completed With Ease
 2  | 2015-02-12 | Action Not Completed | Difficulty Performing

我已经看过,但找不到任何正常工作的东西。

2 个答案:

答案 0 :(得分:0)

按列分组应该是唯一的,然后在其他列上使用聚合函数,例如max()

select id, 
       max(date) as date, 
       max(description) as description, 
       max(notes) as notes
from your_table
group by id

答案 1 :(得分:-1)

小提琴:http://sqlfiddle.com/#!9/305b7/1

SELECT id, 
  max(`date`),
  GROUP_CONCAT(Description),
  GROUP_CONCAT(Notes)
FROM table1
GROUP BY id