MySQL explains the example of conditional judgment function.
This article mainly introduces MySQL to explain the conditional judgment function example, has a certain reference value, interested friends can refer to, I hope you read this article after a lot of gains, the following let Xiaobian take you to understand.
Function:
IF(expr,v1,v2) function
(2)IFNULL(v1,v2) function
â-o January (3)
(Related free learning recommendation: mysql video tutorial)
(1)if(expr,v1,v2) function
In the if(expr,v1,v2) function, return v1 if expr is true(expr0 and eprnull), otherwise return v2.
[Example] Use the if() function to make conditional judgments. The SQL statement is as follows:
mysql> select if(1>2,2,3), -> if(1 if(strcmp('test','test1'),'no','yes');+-------------+--------------------+---------------------------------------+| if(1>2,2,3) | if(1 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)(3)case function
1.case expr when v1 then r1 [ when v2 then r2] [else rn] end
This function says that if expr is equal to vi, it returns the result after then, and if expr is not equal to all values, it returns m after else.
[Example] Use case value when statement to execute branch operation. 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
This function indicates that if a vi value is true, it returns the result after the corresponding position then, and if all values are not true, it returns rn after else.
The default return value type of a case expression is any compatible set type of return values, depending on the context.
[Example] Use case when statement to execute branch operation. SQL statement is as follows:
mysql> select case when 1