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

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

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


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


------解决方案--------------------
case返回的是一个确定的标量值,30,50这显然不是一个值
select * from tb where (type_order='a' and status in(30,50)) or
(type_order='b' and status in(20,30) )
------解决方案--------------------
引用:

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)+',%'