Troubleshooting MySQL - error to solve the problem MySQL GROUP BY clause is not included in the non-aggregate columns

Troubleshooting - solving MySQL non-polymeric columns that are not included in the GROUP BY clause error problem

By: grant customer QQ : 1033553122

 

test environment

win10

MySQL 5.7

 

Problem Description:

Mysql query like the following,

SELECT id, name, count(*) AS cnt FROM case_table GROUP BY name

Error, as follows:

服务器内部错误 (1055, "Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'case_table.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by")

 

Reason: the presence of non-polymeric column id, not included in the GROUP BY clause.

 

But in this case, other places need to use the id column, can not be removed, then it supposed to do? as follows

 

Solution

method 1

Queries the global sql_mode

SELECT @@GLOBAL.sql_mode;

or

SELECT @@sql_mode

 

 

Query results are as follows:

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';
SET SESSION
 

 

 

Copy query result value, and then set the GLOBAL sql_mode, SESSION sql_model remove the results of the query "ONLY_FULL_GROUP_BY," as follows:

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';

SET SESSION sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

Note: This method is only used for temporary changes restart mysql, the above invalidation.

 

Method 2

When you start mysql, increase sql_model options, as follows:

mysqld --sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" [...其它选项]

 

Method 3:

Under linux service, modify my.conf (modified my.ini under Windows), under the [mysqld] nodes, most sql_mode added to the end configuration, as follows:

...

[mysqld]

...

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 www.cnblogs.com/shouke/p/11569464.html