mysql命令gruop by报错this is incompatible with sql_mode=only_full_group_by

The following error occurs when searching or inserting data in the mysql tool:
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'database_tl.emp.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
 
reason:
Take a look at the syntax of group by:
select select the column in the group + aggregate function from table name group by grouped column 
From the grammatical point of view, the grouping is performed first, and then the columns to be retrieved are determined. The retrieved columns can only be selected from the columns that participate in the grouping.
My current Mysql version is 5.7.17,
Let's take a look at the meaning of ONLY_FULL_GROUP_BY: for the GROUP BY aggregation operation, if the column in the SELECT does not appear in the GROUP BY, then this SQL is illegal, because the column is not in the GROUP BY clause, that is to say, it is found. The column must appear after group by otherwise an error will be reported, or the field appears in the aggregate function.
 
View mysql version command: select version();
View the sql_model parameter command:
SELECT @@GLOBAL.sql_mode;
SELECT @@SESSION.sql_mode;
Discover:
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
The first item enables ONLY_FULL_GROUP_BY by default,
 
Solution:
1. Select only the columns that appear after group by, or add aggregate functions to the columns; (not recommended)
2. Command line input:
set @@GLOBAL.sql_mode='';
set sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 
ONLY_FULL_GROUP_BY is turned off by default!
 
At this time, use the tool to select
SELECT @@sql_mode;
SELECT @@GLOBAL.sql_mode;
 
It is found that ONLY_FULL_GROUP_BY no longer exists, and it feels OK. But if you restart the Mysql service, you will find that ONLY_FULL_GROUP_BY will still exist
 
If you want to completely solve this problem, you have to change the my.ini configuration (if your mysql does not have this file, change my-default.ini to my.ini, my version has no my.ini configuration problem)
 
Add under [mysqld] and [mysql]
SET sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
The following error occurs when searching or inserting data in the mysql tool:
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'database_tl.emp.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
 
reason:
Take a look at the syntax of group by:
select select the column in the group + aggregate function from table name group by grouped column 
From the grammatical point of view, the grouping is performed first, and then the columns to be retrieved are determined. The retrieved columns can only be selected from the columns that participate in the grouping.
My current Mysql version is 5.7.17,
Let's take a look at the meaning of ONLY_FULL_GROUP_BY: for the GROUP BY aggregation operation, if the column in the SELECT does not appear in the GROUP BY, then this SQL is illegal, because the column is not in the GROUP BY clause, that is to say, it is found. The column must appear after group by otherwise an error will be reported, or the field appears in the aggregate function.
 
View mysql version command: select version();
View the sql_model parameter command:
SELECT @@GLOBAL.sql_mode;
SELECT @@SESSION.sql_mode;
Discover:
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
The first item enables ONLY_FULL_GROUP_BY by default,
 
Solution:
1. Select only the columns that appear after group by, or add aggregate functions to the columns; (not recommended)
2. Command line input:
set @@GLOBAL.sql_mode='';
set sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 
ONLY_FULL_GROUP_BY is turned off by default!
 
At this time, use the tool to select
SELECT @@sql_mode;
SELECT @@GLOBAL.sql_mode;
 
It is found that ONLY_FULL_GROUP_BY no longer exists, and it feels OK. But if you restart the Mysql service, you will find that ONLY_FULL_GROUP_BY will still exist
 
If you want to completely solve this problem, you have to change the my.ini configuration (if your mysql does not have this file, change my-default.ini to my.ini, my version has no my.ini configuration problem)
 
Add under [mysqld] and [mysql]
SET sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324688157&siteId=291194637