~~~~~~~~~SQL语句关于Where中IN后用Case,马上给分!~~~~~~~

hxm20003 2013-10-29 11:33:28

status in (case type_order when 'A' then 30,50 when 'B' then 20,30 end)


status是int,这样写报错,应该如何写?

...全文
242 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiongxyt2 2013-10-29
  • 打赏
  • 举报
回复
引用 2 楼 szm341 的回复:
case返回的是一个确定的标量值,30,50这显然不是一个值 select * from tb where (type_order='a' and status in(30,50)) or (type_order='b' and status in(20,30) )
++
-Tracy-McGrady- 2013-10-29
  • 打赏
  • 举报
回复

where 1=1
and (case type_order when 'A' then status in(30,50) when 'B' then  status in(20,30) end)
LongRui888 2013-10-29
  • 打赏
  • 举报
回复
引用 楼主 hxm20003 的回复:

status  in (case type_order when 'A' then 30,50 when 'B' then  20,30 end)
status是int,这样写报错,应该如何写?
把语句改一下,试试:
and
(case type_order when 'A' then ',30,50,' 
                 when 'B' then  ',20,30,' 
 end) like '%,'+cast(status as varchar)+',%' 
發糞塗牆 2013-10-29
  • 打赏
  • 举报
回复
30,50是字符串,不是int
szm341 2013-10-29
  • 打赏
  • 举报
回复
case返回的是一个确定的标量值,30,50这显然不是一个值 select * from tb where (type_order='a' and status in(30,50)) or (type_order='b' and status in(20,30) )
發糞塗牆貮號 2013-10-29
  • 打赏
  • 举报
回复
select * from tb where type_order='a' and status in(30,50) union all select * from tb where type_order='b' and status in(20,30)
Andy__Huang 2013-10-29
  • 打赏
  • 举报
回复
引用 2 楼 szm341 的回复:
case返回的是一个确定的标量值,30,50这显然不是一个值 select * from tb where (type_order='a' and status in(30,50)) or (type_order='b' and status in(20,30) )
+10086

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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