请教各位一条查询语句问题,TY!

xiaoyu3362 2012-05-02 04:35:59
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

请各位大侠帮忙!
...全文
155 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoyu3362 2012-05-02
  • 打赏
  • 举报
回复
问题已经解决了 谢谢各位大侠的帮助~ 感激不尽~
小龙卷卷风 2012-05-02
  • 打赏
  • 举报
回复
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
SqlServer2008 2012-05-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

引用 5 楼 的回复:
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 @ta……
[/Quote]


case when then 也可以的。

select id,num=case when num>1 and num<3 then num else 0 end from @tab
rhj_koko 2012-05-02
  • 打赏
  • 举报
回复
select * from (select id,num from num where id>1 and id<3
union all
select id,'0' from num where id<=1 or id>=3) as a order by id
xiaoyu3362 2012-05-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
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 whe……
[/Quote]

您的方法可行,谢谢指教~
SqlServer2008 2012-05-02
  • 打赏
  • 举报
回复

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
China_yuanli 2012-05-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
select id ,(case when num >1 and num <3 then 0 else num end) from XX
[/Quote]

额 写反了
select id ,(case when num >1 and num <3 then num else 0 end) from XX
China_yuanli 2012-05-02
  • 打赏
  • 举报
回复
select id ,(case when num >1 and num <3 then 0 else num end) from XX
xiaoyu3362 2012-05-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
select * from xx as a left join xx as b on a.id=b.id where b.num>1 and b.num<3

未经测试
[/Quote]

您的方法我测试了一下 查询结果是
id num id num
2 2 2 2

bdmh 2012-05-02
  • 打赏
  • 举报
回复
select * from xx as a left join xx as b on a.id=b.id where b.num>1 and b.num<3

未经测试

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧