mysql8中的groupby报错

ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'extra_learn.user.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

 

mysql> explain select * from user group by name order by sex;

改成:

mysql> select * from user order by sex;

依据是官方文档[1]:

Previously (MySQL 5.7 and lower), GROUP BY sorted implicitly under certain conditions. In MySQL 8.0, that no longer occurs, so specifying ORDER BY NULL at the end to suppress implicit sorting (as was done previously) is no longer necessary. However, query results may differ from previous MySQL versions. To produce a given sort order, provide an ORDER BY clause.

 

 

这个报错的出现是为了解决mysql5.7版本以前的如下问题:

mysql> select sno,sname from student;
+------+-------+
| sno  | sname |
+------+-------+
| 001  | tom   |
| 001  | tom2  |
+------+-------+
2 rows in set (0.00 sec)

mysql> select sno,sname from student group by sno;
+------+-------+
| sno  | sname |
+------+-------+
| 001  | tom   |
+------+-------+
1 row in set (0.01 sec)

 

[1]https://dev.mysql.com/doc/refman/8.0/en/order-by-optimization.html

[2]https://blog.csdn.net/qq_38234015/article/details/90017695

你可能感兴趣的:(Mysql8操作与性能优化)