IF(expr1,expr2,expr3)
- expr1 为0或者null或者false,则返回 expr3 ; 否则,返回expr2
SELECT
last_name,
salary,
IF (
salary > 10000,
'高工资',
'低工资'
) AS 工资水平
FROM
employees;
SELECT IF(0,1,2);
SELECT IF(NULL,'refine','lily');
SELECT IF(FALSE,'refine','lily');
SELECT IF(2>1,'refine','lily');
IFNULL(expr1,expr2)
- 如果expr1 不是NULL,则返回第一个expr1 ,否则返回expr2;
SELECT last_name,commission_pct,IFNULL(commission_pct,0) FROM employees;
CASE WHEN … THEN …WHEN … THEN … ELSE … END
SELECT
last_name,
salary,
CASE
WHEN salary >= 20000 THEN
'精英'
WHEN salary >= 10000 THEN
'白领'
WHEN salary >= 6000 THEN
'工薪'
ELSE
'贫民'
END
FROM
employees;
CASE … WHEN … THEN … WHEN … THEN … ELSE … END
SELECT
employee_id,
last_name,
job_id,
salary,
CASE job_id
WHEN 'IT_PROG' THEN
salary + 5000
WHEN 'PU_CLERK' THEN
salary + 3000
WHEN 'ST_MAN' THEN
salary + 2000
ELSE
salary
END "details"
FROM
employees;