1、如果为空返回0
select ifnull(null,0)
应用情景,如果在进行右连接或者左连接时,有一些为空的字段,可以进行这样的处理。
select ifnull( (B.submission_time-A.submission_time),0) from A right join B on 条件。
2、如果为空返回0,否则返回1
select if(isnull(col),0,1) as col.
MYSQL 中的IFNULL函数说明:
IFNULL(expr1,expr2)
如果expr1是NULL,IFNULL()返回expr2,否则它返回expr2。
IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境。
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)
如果expr1是TRUE,那么IF()返回expr2,否则它返回expr3。
IF()返回一个数字或字符串值,取决于它被使用的上下文。
mysql> select IF(1>2,2,3);
->3
mysql> select IF(1<2,'yes','no&