oracle count(null)与count(*)

count()
括号中如果是列名的话则不包含NULL
如果是*字符或常量 则包括NULL
下面做几个小例子来看一下
SQL> create table test(id number,name varchar2(10));
Table created.
SQL> insert into test values(1,'wh');
1 row created.
SQL> insert into test values(2,'wo');
1 row created.
SQL> insert into test(id) values(2);
1 row created.
SQL> insert into test(name) values('ha');
1 row created.
SQL> insert into test values(null,null);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from test;
        ID NAME
---------- ----------
         1 wh
         2 wo
         2
           ha

SQL> select count(1) from test;
  COUNT(1)
----------
         5
SQL> select count(*) from test;
  COUNT(*)
----------
         5
SQL> select count(id) from test;
COUNT(ID)
----------
         3
SQL> select count(name) from test;
COUNT(NAME)
-----------
          3


如果表中没有主键,那么count(1)比count(*)快
如果有主键,那么count(主键,联合主键)比count(*)快
如果表中只有一个字段,count(*)最快
count(字段名) 不会计算 null

猜你喜欢

转载自haidaoqi3630.iteye.com/blog/1956710
今日推荐