IFNULL无效

时间:2016-01-06 15:26:37

标签: mysql null

您好我正在使用mysqli来替换表中的默认值,如果数据库中的数据为NULL。我已经在PHPmyAdmin上尝试了它并且它正在工作但不在我的代码上:(

这是我的SELECT查询:

$query="SELECT pro_id, pro_name, unit_name, cat_name, IFNULL(quantity,'empty') AS quantity FROM products, unit, categories WHERE products.unit=unit.unit_id AND products.pro_cat=categories.cat_id";

1 个答案:

答案 0 :(得分:2)

如果您的评论之一似乎表明,您收到的错误是:

Incorrect parameter count in the call to native function 'ISNULL'
那么这只是一个简单的拼写错误。 ISNULLIFNULL不同。

如果一个参数为空,前者返回一个真值。

如果第一个参数为null,则后者返回第二个参数,否则返回第一个参数。

如果将以下代码放入SqlFiddle:

,可以看到这一点
-- DDL
create table xyzzy (plugh int);
insert into  xyzzy (plugh)       values (null);
insert into  xyzzy (plugh)       values (42);

select plugh, isnull(plugh)    from xyzzy;
select plugh, ifnull(plugh,-1) from xyzzy;
select plugh, isnull(plugh,-1) from xyzzy;

输出与前两个select语句一样,而第三个产生您描述的错误:

plugh   isnull(plugh)
------  -------------
(null)  1
42      0

plugh   ifnull(plugh,-1)
------  ----------------
(null)  -1
42      42

Incorrect parameter count in the call to native function 'isnull'