where 条件 in 判断语句中使用case when

实战干货
case when 一般用在 select 后面或者where条件的=后面,如:
select case when '${unit}'='单' then order_num else quantity_num 
from a
where project = case when '${type}'='T1' then 'P1' else 'P2' 
如果type是一个特殊值时,需要使用where条件in逻辑筛选数据,那就会有下面的写法:
where project in (case when '${type}'='ALL' then (
	select project from A
) else 'p2')
因为case when 只能返回单行数据,所以这样写会报出单行和多行的错误提示。
干货:
where project in (
	select distinct
	case when '${type}'='ALL' then project else 'P2' 
	from A
)
问题解决。
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值