积极答复者
请教这条语句如何编写

问题
-
表 T 如下所示
a b c d
--- --- ------- -------
001 1 1 null
001 2 1 null
001 3 1 null
001 4 1 null
002 1 1 null001 1 2 null
001 2 2 null
001 3 2 null
001 4 2 null
002 1 2 null001 1 11 null
001 2 11 null
001 3 11 null
001 4 11 null
002 1 11 null
001 3 12 null
002 1 12 null
做查询
1. select * from t where a=1 and b=1a b c d
----------- ----------- ----------- -----------
1 1 1 NULL
1 1 2 NULL
1 1 11 NULL(3 row(s) affected)
2. select * from t where a=1 and b=2
a b c d
----------- ----------- ----------- -----------
1 2 1 NULL
1 2 2 NULL
1 2 11 NULL(3 row(s) affected)
3. select * from t where a=1 and b=3
a b c d
----------- ----------- ----------- -----------
1 3 1 NULL
1 3 2 NULL
1 3 11 NULL
1 3 12 NULL(4 row(s) affected)
4. select * from t where a=1 and b=4
a b c d
----------- ----------- ----------- -----------
1 4 1 NULL
1 4 2 NULL
1 4 11 NULL(3 row(s) affected)
5. select * from t where a=2 and b=1
a b c d
----------- ----------- ----------- -----------
2 1 1 NULL
2 1 2 NULL
2 1 11 NULL
2 1 12 NULL(4 row(s) affected)
如何编写语句让 查询1,2,4中的c=11时d=123 ,然后查询3,5中的 c=11时d=11,c=12时d=12 ?
答案
-
一句SQL貌似不行,建议您先下SQL判断是否有12
如果有,则case c when 11 then 11 when 12 then 12 end as d
如果没有,则case c when 11 then 123 end as d- 已标记为答案 Hong-Gang Chen - MSFTModerator 2009年10月9日 2:21
全部回复
-
一句SQL貌似不行,建议您先下SQL判断是否有12
如果有,则case c when 11 then 11 when 12 then 12 end as d
如果没有,则case c when 11 then 123 end as d- 已标记为答案 Hong-Gang Chen - MSFTModerator 2009年10月9日 2:21