函數:
(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)
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
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教學(影片)
以上是MySQL講解條件判斷函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!