COUNT(*) 返回多行而不是一行

2021-12-30 00:00:00 count mysql

当我只需要查询生成的总行数时,为什么 COUNT() 返回多行?

Why does COUNT() return multiple rows when I just need the total count of how many rows my query generates?

应该返回 1078.

推荐答案

COUNT() 按预期工作.当您放置 group by 子句时,count() 会为您提供 GROUP BY 的结果.如果您希望获取包含 group by 的查询中的行数,请将其用作子查询.

The COUNT() is working as expected. When you put a group by clause, the count() gives you the result for GROUP BY. If you wish to get the count of rows in a query that includes group by, use it as a subquery instead.

类似于:

SELECT COUNT(*) FROM (SELECT * FROM `table`
                      GROUP BY `column1`) AS `a`

相关文章