日期:2014-05-18  浏览次数:20546 次

50分,小题求助.关于Select查询结果中加入默认值
我想在查询的结果中,加入默认值null,例子如下(表T_table有三列a,b,c):
select a,b from T_table where c in (14343802,14343801) 
in中的数据是我随便举的,在实际的数据表中当C=14343802时,表T_table中a,b有对应的数据,而当c=14343801时,没有对应的数据.
我想得到的结果:  
a b
1002 56
null null

不想得到的结果:
a b
1002 56

也就是当C=14343801时,因为对应的a,b没有数据,查询出的结果中以null,null代替.请问如何实现?谢谢,在线等,解决就给分.

------解决方案--------------------
select '14343802' as c into #tmp 
union all 
select '14343801' as c 

select a,b from T_table tb left join 
#tmp tp on tb.c=tp.c
drop table #tmp
------解决方案--------------------
select t2.a,t2.b from
(select 14343802 c union all select 14343801) t1
left join
(select a,b,c from T_table where c in (14343802,14343801)) t2
on t1.c = t2.c

------解决方案--------------------
SQL code

select a=case a when '' then null else a end,
       b=case b when '' then null else b end 
from T_table where c in (14343802,14343801)

------解决方案--------------------
SQL code

select 
t2.a,t2.b
from 
(select 
14343802 as col
union 
select 14343801)t1
left join 
T_table  t2 
on t1=col=t2.c