SELECT IF(10<5,‘大‘,‘小‘); #第一个表达式为真则返回第二个参数否则返回第三个
#案例:
SELECT
last_name,
commission_pct,
IF(commission_pct IS NULL,‘没奖金‘,‘有奖金‘)
FROM
employees;
case函数: 类似于switch case 的效果
#案例:查询员工的工资部门号=30,工资为1.1倍;40,1.2倍;50,1.3倍;其他部门为原工资
SELECT salary AS 原始工资,department_id,
CASE department_id
WHEN 30 THEN salary*1.1
WHEN 40 THEN salary*1.2
WHEN 50 THEN salary*1.3
ELSE salary #相当于switch中的default:
END AS 新工资 #结束
FROM employees;
case使用2:类似于多重if,then 后显示的为值则不加;如果是表达式则加;
#案例:查询员工的工资的情况
SELECT
salary,
CASE
WHEN salary >20000 THEN ‘a‘
WHEN salary >15000 THEN ‘b‘
WHEN salary >10000 THEN ‘c‘
ELSE ‘d‘
END AS 等级
FROM
employees;
原文:https://blog.51cto.com/14437184/2435964