日期:2014-05-17  浏览次数:20440 次

请教各位一条查询语句问题,TY!
id num
1 1
2 2
3 3
select * from XX where num >1 and num <3
结果是
id num
2 2

查询语句如何修改能让id 1,3 同样显示在查询结果中并且num字段为0
想要的结果是这样的
id num
1 0
2 2 
3 0

请各位大侠帮忙!

------解决方案--------------------
select * from xx as a left join xx as b on a.id=b.id where b.num>1 and b.num<3

未经测试
------解决方案--------------------
探讨
select id ,(case when num >1 and num <3 then 0 else num end) from XX

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

declare @tab table
(
    id int,
    num int
)

insert into @tab(id,num) 
select 1,1 
union all
select 2,2
union all
select 3,3

select id,num=isnull((select num from @tab where id=t.id and num>1 and num<3),0) from @tab t