如何在MySQL中透视此表

时间:2012-05-11 07:57:06

标签: mysql pivot-table

你如何在MySQL中透视这个表?它按日期命令排序。

+--------+----------+-----------+
| grpid  | status   | msg       |
+--------+----------+-----------+
|      1 | OK       | text6     |
|      1 | PROGRESS | text5     |
|      1 | PROGRESS | text4     |
|      2 | FAIL     | text3     |
|      2 | PROGRESS | text2     |
|      2 | PROGRESS | text1     |
+--------+----------+-----------+

到这种形式:

+--------+----------+-------------------+
| grpid  | status   | progress          |
+--------+----------+-------------------+
|      1 | OK       | text4,text5,text6 |
|      2 | FAIL     | text1,text2,text3 |
+--------+----------+-------------------+

1 个答案:

答案 0 :(得分:0)

这不是一个支点,只有GROUP BY使用GROUP_CONCAT()

SELECT   grpid,
         GROUP_CONCAT(CASE WHEN status IN ('OK','FAIL') THEN status END) AS status,
         GROUP_CONCAT(msg) AS progress
FROM     my_table
GROUP BY grpid