【问题标题】:need oracle sql returns 1 row when 0 rows are selected选择0行时需要oracle sql返回1行
【发布时间】:2012-05-11 07:00:11
【问题描述】:

写oracle sql让我头疼。当我的 sql 返回 0 行时,我需要它返回“空”而不是返回 0 行。例如,

select name from employee where id=1;

结果:

0 rows selected

我需要它像这样返回:

Empty

已选择 1 行

既然db中没有这样的记录,有没有办法做到这一点?我确实需要它返回一个值。谢谢!

【问题讨论】:

    标签: sql oracle10g oracle11g


    【解决方案1】:

    更新

     SELECT NVL((select name from employee where id=1), 'Empty') name from dual
    

    感谢@Samual 的想法!!

    【讨论】:

    • 那行不通。没有结果与名称为空不同。按照您的想法,答案是: select nvl((select name from employee where id=1), 'Empty') name from dual 。假设名称不能为空且 id 是唯一的。
    • 如果你想要多行会怎样?
    • 对不起@turbot,我的意思是列。您需要为每列返回一个额外的子选择。
    【解决方案2】:
    select name 
    from employee 
    where id=1
    union all
    select 'Empty'
    from dual
    where not exists (select 1 from employee where id=1)
    

    【讨论】:

      猜你喜欢
      • 2016-10-19
      • 1970-01-01
      • 2015-08-01
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多