在mysql中使用group by,导致错误1055
Using group by in mysql, cause error 1055
当我尝试使用 mysql 将第一种形式 select 转换为第二种形式时
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): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'nctest.pivot.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with 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;
然后就可以了;;
您正在尝试 SELECT
id
列,但您不能,因为它不在 GROUP BY
表达式中。
当我尝试使用 mysql 将第一种形式 select 转换为第二种形式时
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): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'nctest.pivot.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with 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;
然后就可以了;;
您正在尝试 SELECT
id
列,但您不能,因为它不在 GROUP BY
表达式中。