mysql case when案例

本文探讨了SQL中的CASE和WHEN-ELSE结构,如何等效于Java的if-else和switch语句,通过实例解析了如何在数据库查询中实现条件逻辑,并展示了不同场景下的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

case when 条件1 then 值1 when 条件2 then 值2 else 值3 end

这个相当于 java 里面的  if....elseif....elseif.....else

select last_name, salary, case when salary >=10000 then '高级' 
                               when salary >= 5000 then '中级' 
                               when salary >= 3000 then '低级' 
                               else '草根' end "details", 
                          department_id
from employees;

case 表达式 when key1 then 值1 when key2 then 值2 else 值3 end

这个相当于java 里面的 switch 值 case X case Y default Z

select employee_id, salary, case department_id when 10 then salary * 1.1
                                               when 20 then salary * 1.2
                                               when 30 then salary * 1.3
                                               else salary * 1.4 end "details"
from employees;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值