问题就在于:
当id_card字段内容为空(null)时,这个地方计数实际是错误的!
因为count()函数自动将字段id_card的为null的值去掉了!而实际需要将该字段为空的行数据也统计在内。
3.解决方案
使用count(1)解决。
count(1)和count(*),这两个函数计数的时候,都会将null统计在内,也包括重复记录;
count(字段名) ,这个函数会自动将该字段值为null的记录排除在外,也包括重复记录。
2021年9月27日16:32:48
现有一组数据,字段名称为:isupload,其值至少有3种情况,分别为:1,2,空he可能为其它值;
现在需要将不是1且不是2的内容筛选出来,我们第一想要的SQL是酱紫的:
select count(1) from table_name where isupload <> 1 and isupload <> 2;
查询结果,和总数匹配对不上;
select count(1) from table_name;
select count(1) from table_name where isupload = 1;
select count(1) from table_name where isupload = 2;
经排查,发现:第一个SQL语句将字段值为null的数据排除在外了。
select count(1) c from table_name where isupload <> 1 and isupload <> 2
union all
select count(1) c from table_name where isupload is null;
这样,才是字段值既不为1,也不为2的数据。
哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!
相关推荐:
oracle板块
oracle 使用length()函数需要注意的坑!
sql 求和结果总为null的解决办法