当我尝试使用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;
我遇到了一个奇怪的错误
ERROR 1055(42000):SELECT列表的表达式#1不在GROUP BY子句中,并且包含非聚合列&n; nctest.pivot.id'它在功能上不依赖于GROUP BY子句中的列;这与sql_mode = only_full_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;
然后它起作用;;
答案 0 :(得分:0)
您正在尝试SELECT
id
列,但您不能,因为它不在GROUP BY
表达式中。