函数:
(1)IF(expr,v1,v2)函数
(2)IFNULL(v1,v2)函数
(3)CASE函数
(相关免费学习推荐:mysql视频教程)
【例】使用if()函数进行条件判断,SQL语句如下:
mysql> select if(1>2,2,3), -> if(1<2,'yes','no'), -> if(strcmp('test','test1'),'no','yes');+-------------+--------------------+---------------------------------------+| if(1>2,2,3) | if(1<2,'yes','no') | if(strcmp('test','test1'),'no','yes') |+-------------+--------------------+---------------------------------------+| 3 | yes | no |+-------------+--------------------+---------------------------------------+1 row in set (0.00 sec)
【例】使用ifnull()函数进行条件判断,SQL语句如下:
mysql> select ifnull(1,2),ifnull(null,10),ifnull(1/0,'wrong');+-------------+-----------------+---------------------+| ifnull(1,2) | ifnull(null,10) | ifnull(1/0,'wrong') |+-------------+-----------------+---------------------+| 1 | 10 | wrong |+-------------+-----------------+---------------------+1 row in set (0.00 sec)
1.case expr when v1 then r1 [ when v2 then r2] [else rn] end
【例】使用case value when语句执行分支操作,SQL语句如下;
mysql> select case 2 when 1 then 'one' when 2 then 'two' else 'more' end;+------------------------------------------------------------+| case 2 when 1 then 'one' when 2 then 'two' else 'more' end |+------------------------------------------------------------+| two |+------------------------------------------------------------+1 row in set (0.00 sec)
2.case when v1 then rv [when v2 then r2] else rn] end
【例】使用case when 语句执行分支操作,SQL语句如下:
mysql> select case when 1<0 then 'true' else 'false' end;+--------------------------------------------+| case when 1<0 then 'true' else 'false' end |+--------------------------------------------+| false |+--------------------------------------------+1 row in set (0.00 sec)
更多相关免费学习推荐:mysql教程(视频)
Atas ialah kandungan terperinci MySQL讲解条件判断函数. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!