mysql - 条件判断

我在做LeetCode的数据库题的时候,遇到使用条件判断才能解出答案,一时间忘了,所以总结下知识点
if 语句使用模板

# if(a="b",x,y) 如果a 是b的话,返回x,否则返回y,类似Java的三元表达式
select * 
if(表达式,值1,值2) 
from table
where 
case
	when 表达式 thenwhen 表达式 thenwhen 表达式 thenelseend

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; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值