MySQL中if语句的使用方法
文章描述:
Mysql的 if 既可以作为表达式用,也可在存储过程中作为流程控制语句使用。
用户数据表
IF(expr1,expr2,expr3)
如果 expr1 是TRUE (或者expr1 <> 0 且 expr1 <> NULL),则 IF()的返回值为expr2; 否则返回值则为 expr3。IF() 的返回值为数字值或字符串值,具体情况视其所在语境而定。
select *,if(sex=1,"男","女") as gender from tp_user
left join
IF(s.risk_data IS NULL,"'.$xname.'", s.risk_data) AS risk_data
if elseif
select id as 学号, name as 姓名, score as 分数,
(
case
when score >= 90 then '优秀'
when score >= 80 and score < 90 then '良好'
when score >= 60 and score < 80 then '及格'
when score < 60 then '不及格'
end
)
as 等级
from scores;
发布时间:2023/09/28
发表评论