2017-08-25 61 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.

总和