2014-12-07 144 views
0

我有如下表:IFNULL並凝聚似乎不工作

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和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

我使用SQLite'3.8.2 2013年12月6日14點53分三十〇秒27392118af4c38c5203a04b8013e1afdb1cebd0d' – Richard 2014-12-07 23:00:29

+0

升級到SQLite的'3.8.7.2情節中字20時57分56秒2ab564bf9655b7c7b97ab85cafc8a48329b27f93'沒有任何改變。 – Richard 2014-12-07 23:22:30

+0

'SELECT DISTINCT typeof(duration)FROM calls'的輸出是什麼? – 2014-12-08 08:26:58

回答

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值。

+0

現在我正在使用它們!數據庫部分是通過別人的Python項目中重複使用的代碼創建的,並且打印出「NULL」字符串。在Python中將其切換爲'None'會導致'sqlite3'庫打印_real_'NULL'值。 – Richard 2014-12-08 16:50:08