COUNT(*) 返回多行而不是一行 [英] COUNT(*) returning multiple rows instead of just one

查看:43
本文介绍了COUNT(*) 返回多行而不是一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我只需要查询生成的总行数时,为什么 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`

这篇关于COUNT(*) 返回多行而不是一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆