MySQL出现SELECT list is not in GROUP BY clause..

[tap]

Error

The execution SQL error is as follows:

SELECT student.s_no,student.s_name,SUM(result.mark) FROM student,result WHERE student.s_no=result.s_no GROUP BY student.s_no
> 1055 - Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db_ketest.student.s_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
> 时间: 0.081s

the reason

MySQL 5.7.5 and above have dependency detection function. If the ONLY_FULL_GROUP_BY SQL mode is enabled (enabled by default), MySQL will reject select lists, HAVING conditions, or ORDER BY list queries that reference non-aggregate columns that are neither named in the GROUP BY clause, nor are they functionally dependent on them. (Before 5.7.5, MySQL did not detect functional dependencies, and ONLY_FULL_GROUP_BY was not enabled by default.)

Solution

Method 1:
Use command line or database client to execute SQL
1.SQL statement, select @@global.sql_mode query

mysql> select @@global.sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@global.sql_mode                                                                                                                         |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| 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 |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.06 sec)

2. Remove ONLY_FULL_GROUP_BY and reset the value

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

Method two
vi modify the MySQL configuration file my.cnf

[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

Save the configuration file and restart the MySQL service: service mysql restart
==End==

Guess you like

Origin blog.51cto.com/13771903/2595067