Usage instructions
1 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.
For example:
The code is as follows |
Copy Code |
Select Ifnull (col1, ' Default-value '), col2 from test; |
When the col1 field of the test table is NULL, the result returned by the database is Default-value, otherwise the value of itself is returned. However, when the value of the Col1 field is an empty string ("), the result returned is an empty string because the null character is not NULL. If you need to replace the empty string or null value with Default-value, obviously ifnull is not possible, but use the case time statement can be done, the example is as follows:
The code is as follows |
Copy Code |
Select C1, (Case when c2 = ' ' or C2 are null then ' default-value ' else C2 end) From Test; |
Let's give some examples:
The code is as follows |
Copy Code |
Mysql> SELECT ifnull (1,0); +-------------+ | Ifnull (1,0) | +-------------+ | 1 | +-------------+ 1 row in Set
|
Since EXPR1 is 1 and is not NULL, the function returns 1. We can try again, if let Expr1 be NULL, whether can return the second parameter? Let Expr1 = 1/0, because the divisor is 0, the result is NULL.
The code is as follows |
Copy Code |
Mysql> SELECT ifnull (1/0, ' nowamagic '); +-------------------------+ | Ifnull (1/0, ' nowamagic ') | +-------------------------+ | Nowamagic | +-------------------------+ 1 row in Set |
If the previous argument is NULL, the second argument is returned nowamagic.
This function is not difficult to understand, why do you want to introduce this function? This is to tell you the basics of the advanced hacker technology under MySQL, so you can try to understand the first thing
The code is as follows |
Copy Code |
UPDATE table SET views = ' 1 ' WHERE id = -2441 OR (ORD (SELECT ifnull (CAST (FirstName as CHAR), 0x20) from Nowamagic ' ORDER by ID LIMIT 1,1, 2,1) ' >112) # |
It uses the Ifnull function, do you understand its function?