koko的oracle杂货铺

 

select count(*) into 会出现 no_data_found吗?


select count(*) into 会出现 no_data_found吗?

很久以前就遇到这个问题了,今天又碰到了,记录下。

在遇到这个问题之前,我会非常肯定说:不可能。

看个测试:

SQL> desc t
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                                 NUMBER

SQL>
SQL>
SQL>
SQL> select * from t;

        ID
----------
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10

10 rows selected.

SQL> select count(*) from t;

  COUNT(*)
----------
        10

SQL> select count(*) from t where id =0;

  COUNT(*)
----------
         0

SQL> select count(*) from t where id = 0  group by id; 

no rows selected



SQL> select count(*) from t where id=1 group by id;

  COUNT(*)
----------
         1

 

事实就是这样,没有什么不可能。

补充一句,这里要区分“空值”和“空记录”。

 
 
 
 
评论:

这种情况很正常,NO DATA FOUND是在分组前发生的。COUNT(*)是分组的函数,所以出现NO DATA FOUND是很正常的,这和COUNT(*)没关系。

发表于 白鳝 在 2008年12月30日, 09:38 上午 CST #

select count(*) from (select count(*) from t where id = 0 group by id);

发表于 Kuru 在 2008年12月31日, 03:23 下午 CST #

发表一条评论:
  • HTML语法: 启用
 

Valid XHTML or CSS?

[This is a Roller site]
Theme by koko.
 
© koko