case when [val1] then [res1] when [val2] then [res2] … else [default] end
如果 val1 为 true, 返回 res1,…,否则返回 default 默认值
case [expr] when [val1] then [res1] when [val2] then [res2] … else [default] end
如果 expr 的值等于 val1,返回 res1,…,否则返回 default 默认值
-- if(value, t, f)selectif(true,'True','False');selectif(false,'True','False');-- ifnull(value1, value2)select ifnull('OK','Default');select ifnull('','Default');select ifnull(null,'Default');-- case when [val1] then [res1] when [val2] then [res2] ... else [default] endselect
id,
name,(casewhen math >=85then'优秀'when math >=60then'及格'else'不及格'end)as math,(casewhen english >=85then'优秀'when english >=60then'及格'else'不及格'end)as english,(casewhen chinese >=85then'优秀'when chinese >=60then'及格'else'不及格'end)as chinese
from
score;-- case [expr] when [val1] then [res1] when [val2] then [res2] ... else [default] endselect
id,
name,(case address when'北京'then'一线城市'when'上海'then'一线城市'else'二线城市'end)as address
from
tb_emp;