如何计算一年中每个月的三个级别(L1,L2,L3)列和状态P的行数

时间:2016-11-25 05:45:46

标签: php mysql

我有一张这样的表table

enter image description here

所以我想计算三个级别的行(L1,L2,L3)作为列,并且一年中每个月的状态为P

我想要一张像table这样的表:

enter image description here

1 个答案:

答案 0 :(得分:1)

SELECT
    YEAR (date) AS YEAR,
    MONTH (date) AS MONTH,

IF (
    level_id = 'L1',
    count(level_id),
    0
) AS L1,

IF (
    level_id = 'L2',
    count(level_id),
    0
) AS L2,

IF (
    level_id = 'L3',
    count(level_id),
    0
) AS L3
FROM
    stak
GROUP BY
    date

在执行上述查询之前:     enter image description here

在Above查询执行完所需结果后:     enter image description here