2017-08-25 60 views
0

試圖查看列中的內容我最終做了一些計數。列數爲空加外加數不爲零不加積分

該表格有3981行。

但是,已計數的列只顯示其空值和非空值的總數要低得多。

怎麼回事?

MariaDB [mydb]> select count(naf) from client where naf is not null; 
+------------+ 
| count(naf) | 
+------------+ 
|   83 | 
+------------+ 
1 row in set (0.01 sec) 

MariaDB [mydb]> select count(naf) from client where naf is null; 
+------------+ 
| count(naf) | 
+------------+ 
|   0 | 
+------------+ 
1 row in set (0.01 sec) 

MariaDB [mydb]> select count(*) from client; 
+----------+ 
| count(*) | 
+----------+ 
|  3981 | 
+----------+ 
1 row in set (0.01 sec) 

回答

1

下面的查詢誤導你:

select count(naf) from client where naf is null; 

COUNT功能忽略所有NULL值。因此,這個查詢永遠不會返回除零之外的任何值。實際上,client表中有3898 NULL個記錄。要算空,你可以嘗試使用SUM函數:

SELECT SUM(1) FROM client WHERE naf IS NULL; 

這應該是返回的3898.

總和