MySQL查询将表转换为不同的列和其他列计数数组?

时间:2013-05-03 19:42:18

标签: mysql

我的数据表如下:

ID WEEK   RESULT 
1   13     GOOD
2   13     BAD
3   13     GOOD
4   13     WORST
5   14     GOOD
6   14     BAD
7   14     WORST
8   15     BAD
9   15     WORST

我需要一个sql查询来创建一个数组,如下所示:

WWEK   GOOD_RESULT   BAD_RESULT   WORST_RESULT   TOTAL
13            2             1       1              4
14            1             1       1              3
15            0             1       1              2

任何人都可以帮我找一个合适的mysql查询吗?

1 个答案:

答案 0 :(得分:0)

SELECT
  WEEK,
  SUM(RESULT='GOOD') As GOOD_RESULT,
  SUM(RESULT='BAD') As BAD_RESULT,
  SUM(RESULT='WORST') AS WORST_RESULT,
  COUNT(*) As TOTAL
FROM YourTable
GROUP BY
  WEEK

请参阅小提琴here