我有下表:
SELECT * FROM calls LIMIT 10;
time |number |duration|calltype
2011-02-21 19:06:19|11243243569| NULL|missed
2010-07-12 17:47:14|11234123445| NULL|missed
2010-07-01 20:55:14|11234123459| 52|placed
2010-07-24 22:43:53|12342345662| 363|received
2010-07-22 01:05:25|11234123412| NULL|missed
2010-09-16 00:09:03|14321234231| NULL|missed
2010-10-08 16:31:13|14234234253| NULL|missed
2010-10-05 17:34:28|15534535234| NULL|missed
2010-10-22 04:27:15|16127085031| NULL|missed
2010-10-22 23:46:55|12182308974| NULL|missed
我希望将NULL
的有效期替换为0
,但IFNULL
似乎不起作用。
SELECT time,number,IFNULL(duration,0),calltype FROM calls LIMIT 10;
time | number|IFNULL(duration,0)|calltype
2011-02-21 19:06:19|11243243569| NULL|missed
2010-07-12 17:47:14|11234123445| NULL|missed
2010-07-01 20:55:14|11234123459| 52|placed
2010-07-24 22:43:53|12342345662| 363|received
2010-07-22 01:05:25|11234123412| NULL|missed
2010-09-16 00:09:03|14321234231| NULL|missed
2010-10-08 16:31:13|14234234253| NULL|missed
2010-10-05 17:34:28|15534535234| NULL|missed
2010-10-22 04:27:15|16127085031| NULL|missed
2010-10-22 23:46:55|12182308974| NULL|missed
确实,在尝试使用COALESCE()
MAX()
聚合运算符并发现它们也返回NULL时出现了这个问题。
这里可能会发生什么?如何让聚合忽略NULL并IFNULL
工作?
更新
在列上使用typeof
命令会产生以下结果:
SELECT duration,typeof(duration) as typ FROM calls LIMIT 10;
duration|typ
NULL|text
NULL|text
52|integer
363|integer
NULL|text
NULL|text
NULL|text
NULL|text
NULL|text
NULL|text
答案 0 :(得分:0)
ifnull
函数仅适用于NULL类型的值,而不适用于文本'NULL'
。
要将字符串'NULL'
映射到其他内容,您可以使用CASE expression:
SELECT time,
number,
CASE duration WHEN 'NULL' THEN 0 ELSE duration END AS duration,
calltype
FROM calls
使用真正的 NULL值会更好。