which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mod

mysql execution error :

Expression #2 of SELECT list is not in GROUP BY clause and contains
nonaggregated column ‘bcdsystem.cities.city’ which is not functionally
dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by"

1. This error occurs when mysql version 5.7 and above will appear:

   mysql 5.7版本默认的sql配置是:sql_mode="ONLY_FULL_GROUP_BY",这个配置严格执行了"SQL92标准"

2. When the SQL is executed, this reason occurs:

    简单来说就是:输出的结果是叫target list,就是select后面跟着的字段,还有一个地方group by column,就是

    group by后面跟着的字段。由于开启了ONLY_FULL_GROUP_BY的设置,所以如果一个字段没有在target list 

    和group by字段中同时出现,或者是聚合函数的值的话,那么这条sql查询是被mysql认为非法的,会报错误。

3. Solution
1) You can take a temporary solution, but this error will still be reported after restarting mysql

Check the version number: select version();

You can see that my version is above version 5.7,
    insert image description here

Query model: SELECT @@GLOBAL.sql_mode; SELECT @@SESSION.sql_mode;

insert image description here
You can see that the mysql configuration contains sql_mode="ONLY_FULL_GROUP_BY",

Next, execute the following 4 SQL statements to solve the problem ( restarting mysql will fail ):

set sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,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 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 @@sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

2) The final solution: modify the MySQL configuration file

I installed mysql with docker on the linux service, entered the mysql container, and entered the /etc/mysql directory to modify the my.cnf file

vim /etc/mysql/my.cnf

Add the code below [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

[Exception]
But after I cat my.conf, I found that this configuration file does not have [mysqld], and I saw that the configuration file introduced /etc/mysql/conf.d/, /etc/mysql/mysql.conf.d/ files in both directories

After checking that there is a mysqld.cnf file under the imported file mysql.conf.d folder that contains [mysqld] configuration, add the above configuration to the configuration file, save and restart the mysql container, so that the problem can be solved permanently.

ps: Different systems have different mysql configuration file names and paths
1. Mac or Linux file /etc/my.cnf
2. Windows my.ini in the database installation directory

Guess you like

Origin blog.csdn.net/weixin_54514751/article/details/130505055