首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

50分,小题.关于Select查询结果中加入默认值

2012-01-31 
50分,小题求助.关于Select查询结果中加入默认值我想在查询的结果中,加入默认值null,例子如下(表T_table有

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.bfrom (select 14343802 as colunion select 14343801)t1left join T_table  t2 on t1=col=t2.c 

热点排行