使用MYSQL将两行合并为一行

时间:2011-08-01 10:33:19

标签: mysql sql

ID        Column1        Column2        Column3
1         data1                         data3
1                        data2

从上表中,我可以执行单个查询来获取此信息吗? :

ID        Column1        Column2        Column3
1         data1          data2          data3

2 个答案:

答案 0 :(得分:2)

SELECT
  MAX(Column1) AS Column1,
  MAX(Column2) AS Column2,
  MAX(Column3) AS Column3
FROM
  table
GROUP BY
  ID

答案 1 :(得分:1)

SELECT
  GROUP_CONCAT(Column1) AS Column1,
  GROUP_CONCAT(Column2) AS Column2,
  GROUP_CONCAT(Column3) AS Column3
FROM table
GROUP BY ID