Function:
(1)IF(expr,v1,v2) function
(2)IFNULL(v1,v2) function
(3 )CASE function
(Related free learning recommendations: mysql video tutorial)
[Example] Use the if() function to perform conditional judgment. The SQL statement is as follows:
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)
[Example] Use the ifnull() function to perform conditional judgment. The SQL statement is as follows:
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
[Example] Use case value when statement to perform branch operation, the SQL statement is as follows;
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
[Example] Use the case when statement to perform branch operations. The SQL statement is as follows:
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)
More related free learning recommendations: mysql tutorial( video)
The above is the detailed content of MySQL explains conditional judgment function. For more information, please follow other related articles on the PHP Chinese website!