Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL逻辑计数操作_Mysql_Sql_Count - Fatal编程技术网

MySQL逻辑计数操作

MySQL逻辑计数操作,mysql,sql,count,Mysql,Sql,Count,我在MySQL中运行了三个SQL查询,但有一个逻辑问题 select count(*) from keeper where code!=''; -- result1=2893193 select count(*) from keeper where code=''; -- result2=66 select count(*) from keeper; -- result3=3481069 我期望result1+result2=result3,但事实上,result1+result2

我在MySQL中运行了三个SQL查询,但有一个逻辑问题

select count(*) from keeper where code!=''; -- result1=2893193
select count(*) from keeper where code=''; -- result2=66
select count(*) from keeper; -- result3=3481069

我期望
result1+result2=result3
,但事实上,
result1+result2
。这是为什么?

始终使用
is NuLL
ans
is NOT NuLL
分别获取准确的空记录和非空记录。它同时检查空值和空值

请尝试以下内容:

select count(*) from keeper where code is NULL;

或者,您可以使用:

select  count(*) from keeper where LENGTH(COALESCE(code ,'')) = 0

将为代码提供一个“空”值的所有记录,将NULL视为空。

始终使用
IS NULL
ans
IS NOT NULL
分别获取准确的空记录和非空记录。它同时检查空值和空值

请尝试以下内容:

select count(*) from keeper where code is NULL;

或者,您可以使用:

select  count(*) from keeper where LENGTH(COALESCE(code ,'')) = 0

将为代码提供一个“空”值的所有记录,将NULL视为空。

使用
不为空
为空
此外,
='
将确保您获得的所有行都是空的,就像您正在查找的一样,或者将列设置为空

SELECT count(*) FROM keeper WHERE code!='' OR code IS NOT NULL;
SELECT count(*) FROM keeper WHERE code = '' OR code IS NULL

除了使用
=''
之外,使用
不是空的
是空的
将确保您得到的所有行都是空的,就像您正在查找的一样,或者将列设置为空

SELECT count(*) FROM keeper WHERE code!='' OR code IS NOT NULL;
SELECT count(*) FROM keeper WHERE code = '' OR code IS NULL
攻击

NULL和“”是两个不同的东西。NULL被认为既不等于也不等于
,因此您的两个查询都不会返回它。我猜第三次查询返回的额外500000条记录已
code
设置为NULL。您可以使用
为空
为非空
测试空字段。如果您这样做:

SELECT count(*) from keeper where code!='';
SELECT count(*) from keeper where code='';
SELECT count(*) from keeper where code IS NULL;
这三个结果加起来应该等于表中的行总数

攻击

1.   select count(*) from keeper where code!=''
2.   select count(*) from keeper where code=''
2.5. select count(*) from keeper where code is null
3.   select count(*) from keeper
NULL和“”是两个不同的东西。NULL被认为既不等于也不等于
,因此您的两个查询都不会返回它。我猜第三次查询返回的额外500000条记录已
code
设置为NULL。您可以使用
为空
为非空
测试空字段。如果您这样做:

SELECT count(*) from keeper where code!='';
SELECT count(*) from keeper where code='';
SELECT count(*) from keeper where code IS NULL;
这三个结果加起来应该等于表中的行总数

1.   select count(*) from keeper where code!=''
2.   select count(*) from keeper where code=''
2.5. select count(*) from keeper where code is null
3.   select count(*) from keeper
注意在
3
之前插入的一个。NULL被视为与任何其他值不同的情况,既不等于也不等于任何其他值(包括另一个NULL)


注意在
3
之前插入的一个。NULL被认为是与任何其他值不同的一种情况,既不等于也不等于任何其他值(包括另一个NULL)。

NULL和“”是两个不同的东西
-除非你有一个像Oracle这样的头脑死亡的DBMS,它无法分辨区别:-)对不起,永远不能错过一个机会,在一天中收回他们奇怪的决定。
NULL和“”是两个不同的东西
-除非你有一个像Oracle这样的头脑死亡的DBMS,它不能分辨区别:-)对不起,永远不能错过一个机会把他们奇怪的决定带回去。我从来都不能让
为NULL
返回空值,这就是为什么我总是把这两个值都放进去。对于
is not NULL
来说,它实际上是不需要的。我从来没有能够得到
is NULL
返回的值是空的,这就是为什么我总是把这两个值都放进去。如果
不为空