Mysql group by this is incompatible with sql_mode=only_full_group_by Mysql 5.7 version group by problem

tags: mysql group by  ONLY_FULL_GROUP_BY  this is incompatible with sql_

which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

When executing a colleague's sql, I found that group by has a problem.

select max(id) as id,c_service_time,c_adress,c_rfid_sign from t_records group by c_rfid_sign;

The reason is that after 5.7, mysql sql_mode has ONLY_FULL_GROUP_BY mode limitation, that is, the query column must be in group by, or aggregate column (using max, min, avg, sum aggregate function)

There are two types of solutions

First, modify the sql sql_mode settings

First, you can execute the following command to view the sql_mode settings.

1、SHOW SESSION VARIABLES;

2、SHOW GLOBAL VARIABLES;
3、select @@sql_mode;

The result is ONLY_FULL_GROUP_BY, STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_ENGINE_SUBSTITUTION

Run the following command to modify sql_mode.


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

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 @@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, modify the query sql

select * from t_records where id in(select max(id) from t_records group by c_rfid_sign );

select * from t_records a where not exists( select * from t_records b where a.c_rfid_sign=b.c_rfid_sign and a.id<b.id);

 

Third, sql_mode description

ReferenceMySQL Query: Order by Clause Is Not in Group by..this is incompatible with sql_mode = only_full_group_by

ONLY_FULL_GROUP_BY: For a GROUP BY aggregate operation, if the column in the SELECT is not in the GROUP
appears in BY, then this SQL is not legal, because the column is not in the GROUP BY clause

NO_AUTO_VALUE_ON_ZERO: This value affects the insertion of self-growth columns. By default, inserting 0 or NULL represents the next self-growth value. If the user
The value you want to insert is 0, and the column is self-growth, so this option is useful.

STRICT_TRANS_TABLES: In this mode, if a value cannot be inserted into a transaction table, the current operation is interrupted, and no restrictions are imposed on the non-transactional table.
NO_ZERO_IN_DATE: In strict mode, dates and months are not allowed to be zero

NO_ZERO_DATE: Set this value, the mysql database does not allow the insertion of a zero date, inserting a zero date will throw an error instead of a warning.

ERROR_FOR_DIVISION_BY_ZERO: In the INSERT or UPDATE process, if the data is divided by zero, an error is generated instead of a warning. Such as
If this mode is not given, then MySQL returns NULL when the data is divided by zero.

NO_AUTO_CREATE_USER: Disable GRANT to create a user with a blank password

NO_ENGINE_SUBSTITUTION:
An error is thrown if the required storage engine is disabled or not compiled. When this value is not set, replace it with the default storage engine and throw an exception

PIPES_AS_CONCAT:
treats "||" as a string concatenation operator instead of an OR operator, which is the same as the Oracle database, and is similar to the string concatenation function Concat.

ANSI_QUOTES: After ANSI_QUOTES is enabled, double quotes cannot be used to reference a string because it is interpreted as an identifier

Intelligent Recommendation

MySQL Query: Order by Clause Is Not in Group by..this is incompatible with sql_mode = only_full_group_by

When using MySQL, the following query is performed: The error message is as follows: The wrong reason is that my mysql version is 5.7, using the following statement query you know It is set by default...

Solution: MySQL Error 1055: Group by clause; this is incompatible with sql_mode = only_full_group_by

When using MySQL to perform Group By query statement, error: problem causes The SQL standard does not allow the Select list, a Having condition statement, or a grouse in the Group By in the Order by s...

MySQL error: in aggregated query without group by, incompatible with sql_mode = only_full_group_by

Scenario: The same product, different models, there are different prices, such as 1 mobile phone has 128G, 2,256g, 2500 yuan, but the product display page, the price shows 2,000 yuan, if it is a produ...

MySql: In aggregated query without GROUP BY...;this is incompatible with sql_mode=only_full_group_by

wrong reason This is caused by the version features above MySQL5.7. solution Execute the following code Run and then you can, finish....

Mysql 8.0.13 (8. +) version this is incompatible with sql_mode = only_full_group_by problem

Problem Description: When the online data is copied to the local database code, the following error is reported when a group_by statement is involved in a Group_by statement: Focusing onsql_mode=only_...

More Recommendation

Group BY problem in mysql 5.7+ version

1. Modify My.cnf (under Windows is a My.ini) configuration file, delete the only_full_group_by, if there is no only_hull_group, run 2. Copy ONLY_FULL_GROUP_BY, such as: Add it to My.ini files, restart...

MySql group error sql_mode=only_full_group_by

MySql reports an error sql_mode=only_full_group_by Prospect introduction problem solve Prospect introduction There is a long long ago project. As the purchased Alibaba Cloud database expired and no on...

mysql group sql_mode=only_full_group_by issue

1055 - Expression #1 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_m...

Solve MySQL 5.7.9 version SQL_MODE = ONLY_FULL_GROUP_BY problem, this is incompatible with sql_mode = only_full_group_by error

============================== Record start ================== ===== This article mainly introduces the solution to the Mysql 5.7.9 version SQL_Mode = Only_Full_Group_by, the friends who need you can ...

Solution MySQL 5.8.x version sql_mode = only_full_group_by problem, this is incompatible with sql_mode = only_full_group_by error error error error error

Project scene: Run this SQL script error with DBeaver today script: Problem description: sql_mode = only_full_group_by Reason analysis: MySQL8.x version defaults to open onLY_FULL_GROUP_BY The officia...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top