两个mysql的函数

ifnull:如果第一个参数值为NULL,输入第二个参数值

比如
mysql> select ifnull(null, 1);
+-----------------+
| ifnull(null, 1) |
+-----------------+
|               1 |
+-----------------+
mysql> select ifnull(2
, 1);
+--------------+
| ifnull(2, 1) |
+--------------+
|            2 |
+--------------+
1 row in set
mysql> select ifnull(null, 0
);
+-----------------+
| ifnull(null, 0) |
+-----------------+
|               0 |
+-----------------+
1 row in set
处理查询单列为NULL时输出0非常方便

COALESCE:输出第一个非NULL的参数
mysql> SELECT COALESCE(NULL,1);
+------------------+
| COALESCE(NULL,1) |
+------------------+
|                1 |
+------------------+
1 row in set

mysql> SELECT COALESCE(NULL,NULL,
1);
+-----------------------+
| COALESCE(NULL,NULL,1) |
+-----------------------+
|                     1 |
+-----------------------+
1 row in set
与ifnull功效差不多,但是对处理多列null的功能

你可能感兴趣的:(mysql)