IFNULL和IF之间的差异

时间:2012-06-21 22:26:37

标签: mysql

以下两个陈述之间是否存在差异:

mysql> EXPLAIN SELECT IF(arms IS NULL, 'asdf', arms) FROM limbs;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | limbs | ALL  | NULL          | NULL | NULL    | NULL |   12 |       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)

mysql> EXPLAIN SELECT IF(arms IS NULL, 'asdf', arms) FROM limbs;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | limbs | ALL  | NULL          | NULL | NULL    | NULL |   12 |       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)

一个人表现得更好还是优于另一个?或者它们是否相同?

1 个答案:

答案 0 :(得分:1)

IFNULL只是IF的{​​{3}},这是ANSI SQL CASE的语法糖。

因此,您可以根据自己的特殊需求使用自己喜欢的任何一种。

PS:EXPLAIN不依赖SELECT