转换为数据透视表

时间:2018-10-02 17:44:00

标签: mysql database pivot

编辑:雷蒙德·尼兰(Raymond Nijland)提出了建议,而问题就在我此端。我把额外的分组放在最后

,正确的语法是

SELECT 
    bkg.status,
count( if( bkg.status = '1', bkg.bookingId, 0 ) ) AS 'confirmed',
count( if( bkg.status = '2', bkg.bookingId, 0 ) ) AS 'Tentative'
FROM 
    tbl_booking bkg

谢谢


我有像这样的表的表数据

enter image description here

我有一个查询

select bkg.status,
count(bkg.status) from tbl_booking bkg
GROUP BY 
    bkg.status;

给出类似

的结果

enter image description here

我想将其转换为枢轴格式

喜欢

1 | 2 | 3 | 4
21| 57| 7 | 14

我尝试了

SELECT 
    bkg.status,
count( if( bkg.status = '1', bkg.bookingId, 0 ) ) AS 'confirmed',
count( if( bkg.status = '2', bkg.bookingId, 0 ) ) AS 'Tentative'
FROM 
    tbl_booking bkg
GROUP BY 
    bkg.status;

但这给了我类似

的结果

enter image description here

如果我尝试按不同的列(例如预订ID)进行分组,则结果会有所不同。我对于将哪个列放在总和中以及将哪个列放在分组中感到困惑

请提出建议

0 个答案:

没有答案