mysql ORDER BY clause is not in GROUP BY clause and contains nonaggregated column

The first one sets the sql_mode of mysql

Log in to mysql client mysql -u root -p

then enter the password

use mysql

First query the current configuration:

select version(), @@sql_mode;
+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| version()  | @@sql_mode                                                                                                                                |
+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| 5.7.33-log | 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 |

Solution:  Just remove ONLY_FULL_GROUP_BY in 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';
 

Then execute select @@sql_mode; or select version(), @@sql_mode; View

+------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode                                                                                                             |
+------------------------------------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+------------------------------------------------------------------------------------------------------------------------+

Then exit out of the mysql client, log in again, and execute select @@sql_mode;

mysql> select @@sql_mode;
+------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode                                                                                                             |
+------------------------------------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Setup succeeded.

Then restart the project and you're good to go.

 The second is to set the sql_mode of mysql

  1. Or modify the my.cnf file /etc/my.cnf

 Common values ​​of sql_mode are as follows:

    1. 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, the detected column must appear after the group by, otherwise an error will be reported, or this field appears in the aggregation function.
     select * from app01_book group by publish_id;#Error
     select publish_id,Avg(price) from app01_book group by publish_id;#Correct, no error will be reported
 
   2. NO_AUTO_VALUE_ON_ZERO:
 
      This value affects the insertion of self-increasing columns. By default, inserting 0 or NULL means generating the next auto-increment value. This option is useful if the user wants to insert a value of 0 and the column is self-increasing.
 
   3. STRICT_TRANS_TABLES: Strict mode
 
      In this mode, if a value cannot be inserted into a transaction table, the current operation is interrupted, and there are no restrictions on non-transaction tables.
 
   4. NO_ZERO_IN_DATE:
 
      In strict mode, the date and month are not allowed to be zero.
 
   5. NO_ZERO_DATE:
 
      Set this value, the mysql database does not allow inserting a zero date, and inserting a zero date will throw an error instead of a warning.
  
   6. ERROR_FOR_DIVISION_BY_ZERO:
 
      During an INSERT or UPDATE, if data is divided by zero, an error is generated instead of a warning. If the mode is not given, MySQL returns NULL when the data is divided by zero.
 
   7. NO_AUTO_CREATE_USER:
 
      GRANT is prohibited from creating users with empty passwords.
 
   8. NO_ENGINE_SUBSTITUTION:
 
      If the required storage engine is disabled or not compiled, an error is thrown. When this value is not set, the default storage engine is used instead, and an exception is thrown
 
   9. PIPES_AS_CONCAT:
 
      Treat "||" as a string connection operator instead of an or operator, which is the same as the Oracle database, and also
 
   10. ANSI_QUOTES:
 
      After ANSI_QUOTES is enabled, double quotes cannot be used to quote strings, because it is interpreted as the identifier ORACLE's sql_mode setting is equivalent
 
    : PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, NO_TABLE_OPTIONS, NO_FIELD_OPTIONS , NO_AUTO_CREATE_USER.
 
    If you use mysql, in order to continue to retain the habit of using oracle, you can set the sql_mode of mysql as follows:
 
    add the following configuration in my.cnf
 
    [mysqld]
    sql_mode='ONLY_FULL_GROUP_BY,NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,
    ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,PIPES_AS_CONCAT,ANSI_QUOTES'  

Guess you like

Origin blog.csdn.net/a694704123b/article/details/128011898