【发布时间】:2017-04-30 03:45:20
【问题描述】:
这是不正确的,因为答案是更改全局 sql_mode 设置。 MySQL 基于 functional dependencies and returning arbitrary data 更改了 ONLY_FULL_GROUP_BY 默认值是有充分理由的
我的问题和答案对错误以及如何使用更详细的陈述规避它进行了简短而准确的解释。
感谢阅读,表格如下:
+-----------+-------------+-----------+----------+-------+
| productID | productCode | name | quantity | price |
+-----------+-------------+-----------+----------+-------+
| 1001 | PEN | Pen Red | 5000 | 1.23 |
| 1002 | PEN | Pen Blue | 8000 | 1.25 |
| 1003 | PEN | Pen Black | 2000 | 1.25 |
| 1004 | PEC | Pencil 2B | 10000 | 0.48 |
| 1005 | PEC | Pencil 2H | 8000 | 0.49 |
+-----------+-------------+-----------+----------+-------+
所以如果我运行命令:
SELECT * FROM products GROUP BY productCode;
我收到以下错误:
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'southwind.products.productID' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
因此,根据我在阅读mysql documentation 时所收集到的信息,由于我的 GROUP BY 参数中有重复值,它会引发错误,拒绝任意返回某些内容。我宁愿不禁用此功能,因此非常感谢您了解我缺少什么以使查询工作的任何帮助。
谢谢。
【问题讨论】:
-
那么您希望为每个组返回哪一行,因为您在每个 productCode 中没有相同的行?