mysql> select COUNT(*), * from help_keyword;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* from help_keyword' at line 1
这个没有:
mysql> select COUNT(*), help_keyword.* from help_keyword;
+----------+-----------------+------+
| COUNT(*) | help_keyword_id | name |
+----------+-----------------+------+
| 452 | 0 | JOIN |
+----------+-----------------+------+
1 row in set (0.00 sec)
这是为什么?
最佳答案
因为通过使用 Count(*)
,您告诉数据库您想要一个聚合查询,因此选择中的任何列或表达式不包含某些聚合函数(如 count、min、 max 等)用于定义将执行聚合的“桶”。如果有任何此类表达式,您通常还需要包含一个 Group By
子句来显式列出这些列/表达式。通过包括表的所有列(这是 * 所做的),您说的是对表中的每一行进行聚合,这是没有意义的。 (在任何适当的关系表中,总是只有一行与该行的每个值匹配)
关于mysql - 在同一个 select 子句中使用 count(*) 和 * 会报错。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4422680/