The following is a detailed description of the use of MySQL ifnull function, for your reference to learn, if you have encountered similar problems in MySQL ifnull function use, may wish to see.
MYSQL ifnull (EXPR1,EXPR2)
If EXPR1 is not Null,ifnull () returns EXPR1, it returns EXPR2. Ifnull () returns a numeric 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), then if () returns EXPR2, otherwise it returns EXPR3. IF () returns a numeric 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 value, which means that if you are testing floating-point or string values, you should use a comparison operation to do so.
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 transformed to an integer value, causing the test if (0). This may not be what you expect. In the second case, the comparison tests the original floating-point value to see if it is nonzero, and the result of the comparison is used as an integer.
Case value when [compare-value] and then result [when [compare-value] then result ...] [ELSE result] END
case is [condition] then result [when [condition] then result ...] [ELSE result] END
The first version returns a result, where value=compare-value. In the second version, if the first condition is true, a result is returned. If there is no matching result value, then the result after else is returned. If there is no else part, then NULL is returned.
Mysql> Select Case 1 while 1 then "one" while 2 then "one" and "more" END;
"One"
Mysql> Select Case is 1>0 then "true" ELSE "false" END;
"True"
mysql> Select Case BINARY ' B ' when ' a ' then 1 when ' B ' then 2 END;
, NULL
The following is a detailed description of the use of MySQL ifnull function, for your reference to learn, if you have encountered similar problems in MySQL ifnull function use, may wish to see.
MYSQL ifnull (EXPR1,EXPR2)
If EXPR1 is not Null,ifnull () returns EXPR1, it returns EXPR2. Ifnull () returns a numeric 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), then if () returns EXPR2, otherwise it returns EXPR3. IF () returns a numeric 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 value, which means that if you are testing floating-point or string values, you should use a comparison operation to do so.
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 transformed to an integer value, causing the test if (0). This may not be what you expect. In the second case, the comparison tests the original floating-point value to see if it is nonzero, and the result of the comparison is used as an integer.
Case value when [compare-value] and then result [when [compare-value] then result ...] [ELSE result] END
case is [condition] then result [when [condition] then result ...] [ELSE result] END
The first version returns a result, where value=compare-value. In the second version, if the first condition is true, a result is returned. If there is no matching result value, then the result after else is returned. If there is no else part, then NULL is returned.
Mysql> Select Case 1 while 1 then "one" while 2 then "one" and "more" END;
"One"
Mysql> Select Case is 1>0 then "true" ELSE "false" END;
"True"
mysql> Select Case BINARY ' B ' when ' a ' then 1 when ' B ' then 2 END;
, NULL
The use of MYSQL ifnull functions