select name,data from table where name=1
union all
select name,data from table where name=4
union all
select 4,0 from dual where not exists(select name,data from table where name=4)

解决方案 »

  1.   

    select name,data from table where name=1
    union all
    select 4,0 from dual where not exists(select name,data from table where name=4)
      

  2.   

    To Tongls(编程杀手):
    你那么写是不完全对的,只考虑到无记录的情况,如果存在name=4的记录
    name       data
        1           23
        2           23
        3           23
        4           22
    你的结果是这样?
    1    23正确结果应该这样:
    1    23
    4    22
      

  3.   

    select name,data from table where name = '1'
        union 
    select '4','0' from table
            where not exists (select 1 from table b where b.name = '4');
      

  4.   

    如果房主要求返回4,23

    select name,data from table where name in('1','4')
        union 
    select '4','0' from table
            where not exists (select 1 from table b where b.name = '4');
    好了