What we will discuss with you today is the MySQL string value and its actual expression for functions ifnull () and if () [similar to mssqlserver's isnull ()] the following describes the actual operation steps for correct judgment. FNULL (expr1, expr2) If expr1 is not NULL, IFNULL () returns expr1, otherwise it
What we will discuss with you today is the MySQL string value and its actual expression for functions ifnull () and if () [similar to the isnull () of ms SQL server] the following describes the actual operation steps for correct judgment. FNULL (expr1, expr2) If expr1 is not NULL, IFNULL () returns expr1, otherwise it
What we will discuss with you today is the MySQL string value and its actual expression for functions ifnull () and if () [similar to the isnull () of ms SQL server] the following describes the actual operation steps for correct judgment.
- FNULL(expr1,expr2)
If expr1 is not NULL, IFNULL () returns expr1; otherwise, it returns expr2. IFNULL () returns a number or string value, depending on the context in which it is used.
- MySQL> select IFNULL(1,0);
- -> 1
- MySQL> select IFNULL(0,10);
- -> 0
- MySQL> select IFNULL(1/0,10);
- -> 10
- MySQL> select IFNULL(1/0,’yes’);
- -> ‘yes’
- IF(expr1,expr2,expr3)
IF expr1 is TRUE (expr1 <> 0 and expr1 <> NULL), IF () returns expr2; otherwise, expr3 is returned. IF () returns a number or string value, depending on the context in which it is used.
- MySQL> select IF(1>2,2,3);
- -> 3
- MySQL> select IF(1<2,’yes’,'no’);
- -> ‘yes’
- MySQL> select IF(strcmp(‘test’,'test1′),’yes’,'no’);
- -> ‘no’
Expr1 is calculated as an integer, which means that if you are testing a floating point or string value, you should use a comparison operation.
- MySQL> select IF(0.1,1,0);
- -> 0
- MySQL> select IF(0.1<>0,1,0);
- -> 1
In the first case above, IF (0.1) returns 0, because 0.1 is converted to an integer, resulting in testing IF (0 ). This may not be what you expected. In the second case, compare and test the original floating point value to see if it is non-zero. The comparison result is used as an integer.
- CASE value WHEN [compare-value] THEN result [WHEN [compare-value] THEN result …] [ELSE result] END
- CASE WHEN [condition] THEN result [WHEN [condition] THEN result …] [ELSE result] END
The first version returns result, where value = compare-value. In the second version, if the first condition is true, result is returned. If no matching result value exists, the result after ELSE is returned. If no ELSE part exists, NULL is returned.
- MySQL> SELECT CASE 1 WHEN 1 THEN “one” WHEN 2 THEN “two” ELSE “more” END;
- -> “one”
- MySQL> SELECT CASE WHEN 1>0 THEN “true” ELSE “false” END;
- -> “true”
- MySQL> SELECT CASE BINARY “B” when “a” then 1 when “b” then 2 END;
- -> NULL
The above content is an introduction to the MySQL string value/expression judgment functions ifnull () and if () [similar to the isnull () of ms SQL server]. I hope you will gain some benefits.