我在做LeetCode的数据库题的时候,遇到使用条件判断才能解出答案,一时间忘了,所以总结下知识点
if 语句使用模板
# if(a="b",x,y) 如果a 是b的话,返回x,否则返回y,类似Java的三元表达式
select *
if(表达式,值1,值2)
from table
where
case
when 表达式 then 值
when 表达式 then 值
when 表达式 then 值
else 值
end
eg:
# sales部门去P1地点
# account部门去P2地点
# research部门去P3地点
select
e.empno,e.ename,
case
when d.dname="sales" then "P1"
when d.dname="account" then "P2"
when d.dname="research" then "P3"
end as place
from t_emp e join t_dept on e.deptno=d.deptno;