Discuss / SQL / mysql5.7.23运行下面的SQL执行成功了

mysql5.7.23运行下面的SQL执行成功了

Topic source

stray_bird_

#1 Created at ... [Delete] [Delete and Lock User]

SELECT name, class_id, COUNT(*) num FROM students GROUP BY class_id; 返回值如下,这是为什么 +------+----------+------+ | name | class_id | num | +------+----------+------+ | 小明 | 1 | 4 | | 小白 | 2 | 3 | | 小新 | 3 | 3 | +------+----------+------+

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

打开strict模式:

mysql> SELECT name, class_id, COUNT(*) num FROM students GROUP BY class_id;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.students.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
mysql> select @@global.sql_mode global_mode,@@sql_mode session_mode;
+--------------------------------------------+-------------------+
| global_mode                                | session_mode      |
+--------------------------------------------+-------------------+
|** STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION | STRICT_ALL_TABLES **|

老师 我这边 strict mode 是不是不太对 。我的结果还是 如下

+--------+----------+-----+
| name   | class_id | num |
+--------+----------+-----+
| 小明   |        1 |   4 |
| 小白   |        2 |   3 |
| 小新   |        3 |   3 |

  • 1

Reply