1055:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘text.aparment.id’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
[ SQL语句 ] : select id,house_room,count(*) as mcount from aparment WHERE `borough_id`=47 group by house_room
这个错误是由于你的SQL查询语句在启用了ONLY_FULL_GROUP_BY模式下执行时,不满足MySQL的分组依据(GROUP BY)要求引起的。
当启用ONLY_FULL_GROUP_BY模式时,SELECT语句中的每个非聚合列都必须出现在GROUP BY子句中,或者必须是某个聚合函数的参数。
具体来说,id列不是聚合函数的参数,但却出现在SELECT列表中,同时没有出现在GROUP BY子句中,这就导致了错误。
解决办法:
1、确保所有非聚合列都出现在GROUP BY子句中:
select house_room,count(*) as mcount from aparment WHERE `borough_id`=47 group by house_room
2、使用聚合函数
SELECT MIN(id) AS id, house_room, COUNT(*) AS mcount
FROM aparment
WHERE borough_id = 47
GROUP BY house_room;
3、禁用ONLY_FULL_GROUP_BY模式(不推荐)
虽然你可以禁用ONLY_FULL_GROUP_BY
模式来避免这个错误,但不推荐这样做,因为这可能会导致非确定性的查询结果。
禁用方法如下:
SET sql_mode = (SELECT REPLACE(@@sql_mode, ‘ONLY_FULL_GROUP_BY’, ”));