COUNT(*)是否总是返回结果?

编程入门 行业动态 更新时间:2024-10-27 07:16:46
本文介绍了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(*)是否总是返回结果?

本文发布于:2023-10-15 12:12:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1494299.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:COUNT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!