您的位置:首页 > Web前端

Select COUNT(*) and COUNT(column) are different things!

2014-11-03 16:01 381 查看

Select COUNT(*) and COUNT(column) are different things!

by
Tanel PoderPosted onAugust
21, 2009
Every now and then I see someone wondering why Oracle is “returning wrong results” for some count queries when counting using COUNT(column_name) instead of COUNT(*) or COUNT(<constant>).

Oracle is actually returning correct results, its just that sometimes the people asking the questions haven’t realized that COUNT(column) is something semantically different from COUNT(*).


COUNT(*) operation countsall rows fed to it by execution plan branch under it.

[b][b][b]COUNT(*) operation[/b][/b]统计表对应的段中所有的数据行(包括其上各列的值为空值null的数据行)

[/b]

COUNT(column) operation on the other handcountsall
non-null values in that column from rows fed to it by execution plan branch under it.


[b][b][b][b][b][b][b][b][b]COUNT(column)[/b][/b]
operation[/b][/b][/b]统计表对应的段中[b][b][b][b][b]其列上的值不为空值null的数据行[/b][/b][/b][/b]。[/b][/b][/b][/b]

[/b]

注释:[b]COUNT(column)的一般形式为COUNT(column1||[b][b]column2||......[/b])。[/b][/b]


And here’s a little example:

SQL> select count(*) from v$session;

COUNT(*)
----------
23

SQL> select count(username) from v$session;

COUNT(USERNAME)
---------------
1  <<-- only one non-null value in that column

SQL> select count(nvl(username,'blah')) from v$session; <<-- lets replace NULLs with non-nulls

COUNT(NVL(USERNAME,'BLAH'))
---------------------------
23

参考:
oracle count 0     谷歌
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐