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

被折叠的 条评论
为什么被折叠?



