current position:Home>【 MySQL 】 the seventh part of the process control function

【 MySQL 】 the seventh part of the process control function

2023-01-25 11:21:15Tree_wws

【MySQL】第七部分 流程控制函数



7. 流程控制函数

函数用法
IF(value,value1,value2)如果value的值为TRUE,返回value1,否则返回value2
IFNULL(value1, value2)如果value1不为NULL,返回value1,否则返回value2
CASE WHEN 条件1 THEN 结果1 WHEN 条件2 THEN 结果2 …[ELSE 结果] END [别名]if…else if…else…
CASE expr WHEN 常量值1 THEN 值1 WHEN 常量值1 THEN 值1 … [ELSE 结果] END [别名]switch…case…
# IF(value,value1,value2)
SELECT salary,IF(salary>=8000,'高薪就业','普通就业')as '级别'
FROM employees;

# IFNULL(value1, value2)
SELECT salary,IFNULL(commission_pct,0) as '额外奖金'
FROM employees;

# CASE WHEN 条件1 THEN 结果1 WHEN 条件2 THEN 结果2 ....[ELSE 结果] END [别名]
SELECT last_name,salary,CASE WHEN salary >= 15000
							 THEN '优秀'
							 WHEN salary >= 10000
							 THEN '良好'
							 WHEN salary >= 8000
							 THEN '一般'
							 ELSE '还需要继续努力!'
							 END '详情' 
FROM employees;

# CASE expr WHEN 常量值1 THEN 值1 WHEN 常量值1 THEN 值1 .... [ELSE 结果] END [别名]
SELECT last_name,salary,CASE job_id WHEN 'IT_PROG' 
									THEN 'IT行业'
									WHEN 'AD_VP'
									THEN '广告行业'
									ELSE '其他行业'
									END '行业'
FROM employees;

总结

以上就是今天要讲的内容,希望对大家有所帮助!!!

copyright notice
author[Tree_wws],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2023/025/202301251106214645.html

Random recommended