Mysql met Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggre problem solving

Table of contents

origin

Cause of the problem

solution

method one

way two

Cause:
The project interface reported an error when mysql5.7.x was upgraded to version 8.0.x today. Finally, it was found that when the SQL statement of group by was used, the following problems occurred in MySQL:

Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'xxx' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

The cause of the problem:
After querying the data, it is found that when the group by aggregation operation is performed, the column for the query operation does not appear in the group by, then the SQL is illegal. The main reason is that the setting is not compatible with sql_mode=only_full_group_by.

Through the query, it is found that mysql5.7.5 and above versions will rely on the detection function, that is, only_full_group_by will be enabled by default.

Solution:
First execute the command to check whether only_full_group_by is enabled

select @@global.sql_mode
 
results:
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
If you see ONLY_FULL_GROUP_ BY this, just delete it. See below for details:

Method 1:
Add new configuration under the mysq configuration file, then save and restart mysql

[mysqld]
sql_model=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
方式二:
set @@global.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
注意: 8.0.x的版本不支持NO_AUTO_CREATE_USER,去掉即可

Guess you like

Origin blog.csdn.net/weixin_46016659/article/details/129914881