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是如何工作的?的详细内容。
