当我尝试使用 mysql 将第一种形式选择为第二种形式时
select id, name ,
(case when years=1992 then cost else 0 end) as year1992,
(case when years=1993 then cost else 0 end) as year1993,
(case when years=1994 then cost else 0 end) as year1994
from pivot
group by name;
我遇到了一个奇怪的错误
错误 1055 (42000):SELECT 列表的表达式 #1 不在 GROUP BY 子句中,并且包含在功能上不依赖于 GROUP BY 子句中的列的非聚合列“nctest.pivot.id”;这与 sql_mode=only_full_group_by 不兼容
这里有什么问题?
- - - - - - -更新 - - - - - - - - - - - - -
我发现我在使用 group by 时犯了一个错误。
select id, name ,
sum(case when years=1992 then cost else 0 end) as year1992,
sum(case when years=1993 then cost else 0 end) as year1993,
sum(case when years=1994 then cost else 0 end) as year1994
from pivot
group by id, name;
然后它工作;;