是否有更有效的方法从包含日期列的表而不是使用内联子查询返回多个列?
SELECT (SELECT SUM(`value`) FROM `data` WHERE MONTH(`date`) = 1 AS `Jan`),
(SELECT ...) // Feb, Mar, etc.
因为有12个内联子查询对查询引擎造成负担,对吗?
答案 0 :(得分:4)
SELECT YEAR(`date`) as `YEAR`,
SUM(CASE WHEN MONTH(`date`)=1 THEN `value` ELSE 0 END) AS `JAN`,
...
GROUP BY YEAR(`date`)
答案 1 :(得分:0)
SELECT SUM(`value`) FROM `data` GROUP BY MONTH(`date`)