Jika expr1 bukan NULL, IFNULL() mengembalikan expr1; jika tidak, ia akan mengembalikan expr2.
SELECT IFNULL(1,0);
1
SELECT IFNULL(NULL,10);
10
SELECT IFNULL(1/0,10);
10
SELECT IFNULL(1/0,'yes');
'yes'
The default return type of IFNULL(expr1,expr2) is the more “general” of the two expressions, in the order STRING, REAL, or INTEGER. Consider the case of a table based on expressions or where MySQL must internally store a value returned by IFNULL() in a temporary table:
SELECT IFNULL(1,0);
1
SELECT IFNULL(NULL,10);
10
SELECT IFNULL(1/0,10);
10
SELECT IFNULL(1/0,'yes');
'yes'
In this example, the type of the test column is VARBINARY(4) (a string type).
CREATE TABLE tmp SELECT IFNULL(1,'test') AS test;
DESCRIBE tmp;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| test | varbinary(4) | NO | | | |
+-------+--------------+------+-----+---------+-------+