-1

我有这些代码:

select case 
     when exists (select 1 
                  from table1
                  where id=608071) 
     then column1 
     else 0
  end as abc
from table1 where id=608071;

select decode(count(column1), 0, column1) abc
from (select column1
      from table1
      where id=608071) group by column1; 

他们都没有返回我没有column1,没有0,没有错误。它给我null,即什么都没有。没有行返回。当特定 id 不存在时,我需要得到 0。这里有什么问题?

4

2 回答 2

1

尝试这个!!

select case when exists (select 1 from table1 where id= '608071')  then  (select column1 from table1)  else '0' end as abc from dual
于 2013-03-05T07:08:07.657 回答
0

假设这是您的要求:“当特定 ID 不存在时,我需要获取 0”

select
  nvl(T.COLUMN1, 0) COLUMN1_OR_ZERO
from
  DUAL D left join(
    select
      *
    from
      TABLE1
    where
      id = '608071') T on (1=1)

使用此查询,您只需扫描表一次。

于 2013-03-05T08:11:04.283 回答