现在的位置: 首页 > 综合 > 正文

count(字段)不统计null值

2014年02月28日 ⁄ 综合 ⁄ 共 1226字 ⁄ 字号 评论关闭

在对有null值的字段进行count时,发现count(1)与count(字段)得到的记录不一样。

最后在ITPUB上朋友们的帮助下,解决了。

原帖地址:http://www.itpub.net/thread-1383832-1-2.html

 

结论:1.count(1)与count(*)得到的结果一致,包含null值。
      2.count(字段)不计算null值
      3.count(null)结果恒为0

 

SQL> select * from  v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production

TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

 

 

SQL> desc t_product;
Name         Type         Nullable Default Comments
------------ ------------ -------- ------- --------
PRODUCT_ID   NUMBER(6)    Y                        
PRODUCT_NAME VARCHAR2(50) Y                        
LIST_PRICE   NUMBER(8,2)  Y     

                   

SQL> SELECT *  FROM t_product WHERE list_price IS NULL;

PRODUCT_ID PRODUCT_NAME    LIST_PRICE
---------- --------------- ----------
      3355 HD 8GB /SI     
      1770 8MB Cache /NM  

 

SQL> select count(1), count(*), count(list_price),count(null)
  2    from t_product
  3   where list_price is null;

  COUNT(1)   COUNT(*) COUNT(LIST_PRICE) COUNT(NULL)
---------- ----------    -----------------      -----------
            2             2                            0                   0

 

 

 

 

 

抱歉!评论已关闭.