【发布时间】:2020-12-24 09:46:59
【问题描述】:
SELECT * FROM (SELECT year, amount, month FROM test) AS table1
PIVOT
(SUM(amount)
for month in ('1' as m1, '2' as m2, '3' as m3, '4' as m4)) AS table2
GROUP BY year;
错误代码:1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sum(amount) for month in ('1' as m1, '2' as m2, '3' as m3, '4' as m4)) as ta' at line 4
是什么导致了这个错误?
【问题讨论】:
-
样本数据和预期结果会有所帮助。请使用 {} 格式化您的代码。
-
MySQL 好像不支持
pivot。您可以查看 stackoverflow.com/questions/7674786/… 。您需要使用案例和聚合来实现相同的效果。 -
我建议您在 SQL 中使用大写字母,以帮助阅读您的代码。我建议对其进行修改。