关于case when的问题

lh007 2009-04-23 12:55:51
本人在写一个报表,希望在where语句中使用case when,想得到的结果是,当A字段 = ‘37’时B字段=‘24’或者B字段=‘10’,不知道case when then后面可以等于两个结果吗?,即
where
B字段=
case when A字段 = ‘37’ then '24' or '10'
end
...全文
80 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
不停歇的步伐 2009-04-23
  • 打赏
  • 举报
回复
分开写2个case语句
case when A字段 = ‘37’ then '24' 
case when A字段 = ‘37’ then '10'
damo_baby 2009-04-23
  • 打赏
  • 举报
回复
路过。。。
叶子 2009-04-23
  • 打赏
  • 举报
回复

declare @table table(a int)
insert into @table
select 1 union all
select 37 union all
select 37 union all
select 37 union all
select 67

declare @b table (b int)
insert into @b
select 24 union all
select 10

select a,case a when 37 then (select top 1 b from @b order by rand()) else 0 end as b from @table

/*
a b
----------- -----------
1 0
37 10
37 24
37 10
67 0
*/
sdhdy 2009-04-23
  • 打赏
  • 举报
回复
不可以。
wzy_love_sly 2009-04-23
  • 打赏
  • 举报
回复
create table tb(a int)
insert into tb select '37'
insert into tb select '50'
go
select b=case when a=37 then (
select top 1 a from (
select '24' as a
union
select '10'
)t order by newid()) else a end
from tb


-狙击手- 2009-04-23
  • 打赏
  • 举报
回复
where charindex(','+ B字段+',', case when A字段 = '37' then ',24,10,' else '' end) > 0

34,590

社区成员

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

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