MySQL这与sql_mode = only_full_group_by不兼容

时间:2017-07-27 12:43:11

标签: mysql

SELECT u.*, p.name as plan, COUNT(m.user_id) as totalprojects
FROM users u 
LEFT JOIN plans p ON p.id = u.access
LEFT JOIN members m ON m.user_id = u.id
WHERE u.email = 'email address';

所以我有上述内容,我在Mac上运行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, ONLY_FULL_GROUP_BY';

其中说

  

查询OK,0行受影响(0.00秒)

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)

1 个答案:

答案 0 :(得分:1)

是。提到的查询与sql_mode = only_full_group_by不兼容。如果启用了only_full_group_by,则必须提及您在GROUP BY子句中选择的所有列。在这里,您没有任何GROUP BY子句,并且您也在使用聚合函数(COUNT)。理想情况下,如果启用了only_full_group_by,则此查询将失败。默认情况下,这是从MySQL 5.7开始启用的。

请参阅https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html了解详情