Does COUNT(*) always return a result?(COUNT(*) 是否总是返回结果?)
问题描述
如果我运行一个查询,例如:
If I run a query such as:
SELECT COUNT(*) as num FROM table WHERE x = 'y'
它是否总是返回结果,即使查询不匹配任何记录?或者我是否需要验证并确保返回一行作为结果?
Will it always return a result, even when the query doesn't match any record? Or do I need to validate and make sure a row is returned as the result?
推荐答案
是的,因为它是一个聚合并返回零.除非您添加 GROUP BY 在这种情况下没有结果,因为没有组...
Yes, because it's an aggregate and returns zero. Unless you add GROUP BY in which case no result because there is no group...
MAX/SUM 等将返回 NULL,除非您添加 GROUP BY 然后没有行.只有 COUNT 返回一个没有结果的数字
MAX/SUM etc would return NULL unless you add GROUP BY then no rows. Only COUNT returns a number for no results
编辑,有点晚了:SUM 会像 MAX 一样返回 NULL
Edit, a bit late: SUM would return NULL like MAX
编辑,2013 年 5 月:这适用于所有主要 RDBMS.我想按照 ANSI 标准
Edit, May 2013: this applies to all main RDBMS. I guess as per the ANSI standard
这篇关于COUNT(*) 是否总是返回结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:COUNT(*) 是否总是返回结果?


基础教程推荐
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 在多列上分布任意行 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- oracle区分大小写的原因? 2021-01-01