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)
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)
union all
select 4,0 from dual where not exists(select name,data from table where name=4)
你那么写是不完全对的,只考虑到无记录的情况,如果存在name=4的记录
name data
1 23
2 23
3 23
4 22
你的结果是这样?
1 23正确结果应该这样:
1 23
4 22
union
select '4','0' from table
where not exists (select 1 from table b where b.name = '4');
那
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');
好了