您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MYSQL控制流函数CASE是如何工作的?

2024/6/27 5:44:46发布32次查看
mysql case 语句是一种流程控制功能,允许我们在查询中构建条件,例如 select 或 where 子句。我们有两种 case 语句语法
syntax-1case valwhen compare_val1 then result1when compare_val2 then result2...else resultend
在第一个语法中,如果 val 等于 compare_val1,则 case 语句返回 result1。如果 val 等于 compare_val2,则 case 语句返回 result2,依此类推。
如果 val 与任何compare_val 都不匹配,则case 语句返回 else 子句中指定的结果。
示例mysql> select case 100 -> when 100 then 'it is matched' -> when 200 then 'it is not matched' -> else 'no use of this number' -> end as 'matching the number';+---------------------+| matching the number |+---------------------+| it is matched |+---------------------+1 row in set (0.06 sec)
语法2casewhen condition_1 then result1when condition_2 then result2...else resultend
在第二种语法中,case 语句返回结果 1、结果 2 等。如果条件为真。如果所有条件均不成立,case 语句将返回 else 子句中指定的结果。
示例mysql> select case -> when (100 = 100) then 'it is matched' -> when (200 = 100) then 'it is not matched' -> else 'no use of number' -> end as 'matching the number';+---------------------+| matching the number |+---------------------+| it is matched |+---------------------+1 row in set (0.00 sec)
以上就是mysql控制流函数case是如何工作的?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product