我有以下查询:

SELECT asunto, MONTH(created_at) as mes, COUNT(turno) as numero
FROM tikets
where id_sucursal = 1 and subasunto = 'Pago' GROUP BY asunto, mes ORDER BY mes ASC


这给了我这样的结果:

mysql - 水平排序表-LMLPHP

我想知道如何进行咨询才能使其正常工作或我将要做的事情。感谢您的关注

mysql - 水平排序表-LMLPHP

最佳答案

使用条件聚合:

SELECT t.asunto,
       SUM(MONTH(t.created_at) = 2) as month_2,
       SUM(MONTH(t.created_at) = 3) as month_3,
       SUM(MONTH(t.created_at) = 4) as month_4,
       SUM(MONTH(t.created_at) = 5) as month_5
FROM tikets t
WHERE t.id_sucursal = 1 AND t.subasunto = 'Pago'
GROUP BY asunto;


注意事项:在使用几个月时,通常需要考虑年份。

关于mysql - 水平排序表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43987472/

10-16 14:17