Mysql问题:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause

1 Problem description

Use Navicat to connect to MySQL (version: 8.0.18), execute the query:

select * from t_user WHERE  user_name = 'admin'

There is no problem with the query result, but an error is reported:

[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Screenshot below:
insert image description here

2 reasons

MySql enables the only_full_group_by rule by default from version 5.7 and above.

only_full_group_by rules:
1) The columns after order by must exist after select.
2) The non-aggregate columns that exist after select, having or order by must all appear in the group by clause.

3 solutions

3.1 Follow the only_full_group_by rule;

like:

select t.column1, t.column2 from table as t group by column1

Revise:

select t.column1, t.column2 from table as t group by column1,column2 

3.2 Remove the only_full_group_by rule

Step 1: Query the current rules and execute the query:

SELECT @@sql_mode;

Results of the:
insert image description here

Step 2: Remove the only_full_group_by rule and execute the query:

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

Execution result:
insert image description here
Step 3: Query the current rules again and execute the query:

SELECT @@sql_mode;

Execution result:
insert image description here
It indicates that the only_full_group_by rule has been removed.

4 verification

After removing the only_full_group_by rule, execute the query:

select * from t_user WHERE  user_name = 'admin'

Execution result:
insert image description here
no error is reported, and the problem is solved.

Guess you like

Origin blog.csdn.net/afei8080/article/details/129426075