合并 MySQL 行
Merge MySQL Rows Together
我有一个 table 布局有点像这样:
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
我已经看过了,但找不到任何可以正常工作的东西。
Fiddle : http://sqlfiddle.com/#!9/305b7/1
SELECT id,
max(`date`),
GROUP_CONCAT(Description),
GROUP_CONCAT(Notes)
FROM table1
GROUP BY id
按应该唯一的列分组,然后在其他列上使用聚合函数,例如 max()
select id,
max(date) as date,
max(description) as description,
max(notes) as notes
from your_table
group by id
我有一个 table 布局有点像这样:
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
我已经看过了,但找不到任何可以正常工作的东西。
Fiddle : http://sqlfiddle.com/#!9/305b7/1
SELECT id,
max(`date`),
GROUP_CONCAT(Description),
GROUP_CONCAT(Notes)
FROM table1
GROUP BY id
按应该唯一的列分组,然后在其他列上使用聚合函数,例如 max()
select id,
max(date) as date,
max(description) as description,
max(notes) as notes
from your_table
group by id