MYSQL: How to Use ifnull()

Generally, when we use the ifnull() method, it is similar to the following statement:

IFNULL(expr1,expr2)
If expr1 is not NULL, IFNULL() returns expr1, otherwise it returns expr2. IFNULL() returns a number or string value

select ifnull(name,’no name’) from person;

Convert the null in the query result to the specified string, but in fact, you can also use ifnull() in the where query part:

select * from person where ifnull(name,’no name’)=’no name’;

This kind of use seems a bit stupid, but in some applications it can reduce the amount of code very well.


Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *