Mysql Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column...this is incompatible with sql_mode=only_full_group_by

created at 07-27-2021 views: 146

Error message

select * from `bdetector`
where id in (select id from `bdetector` group by obj_key having count(obj_key) > 1)
> 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'cloud_platform.bdetector.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
> 时间: 0.002s

cause

MySQL 5.7.5 and above functions depend on the detection function. If the ONLY_FULL_GROUP_BY SQL mode is enabled (by default), MySQL will reject select lists, HAVING conditions, or ORDER BY list queries that reference non-aggregate columns that are neither named in the GROUP BY clause, nor are they functionally dependent on them. (Before 5.7.5, MySQL did not detect functional dependencies, and ONLY_FULL_GROUP_BY was not enabled by default.

Solution

step 1: execute the following sql statement

select @@global.sql_mode

RESULT:

ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

Step 2: Remove ONLY_FULL_GROUP_BY and reset the value.

set @@global.sql_mode
='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

second method:

Execute the following sql statement:

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
created at:07-27-2021
edited at: 07-27-2021: