为什么我不能使用 "where R.ApartmentID = null" 从左侧检索所有不匹配的行 table
Why can't I use "where R.ApartmentID = null" to retrieve all non matching rows from the left table
我在 sql 服务器上只有一个月的学习经验,我只是想知道为什么之前的第一个查询会产生正确的结果(即连接两个 table 而只有 select从左边 table 开始没有匹配行的行),而第二个查询 returns 是一个空查询。
第一个
select R.Name, A.Name
from tblResident as R
left join tblApartment as A
on R.ApartmentID = A.ID
where R.ApartmentID is null
第二个
select R.Name, A.Name
from tblResident as R
left join tblApartment as A
on R.ApartmentID = A.ID
where R.ApartmentID = null
Table结构
对于 MySQL:
根据MySQL Documentation on Working with NULL Values:
You cannot use arithmetic comparison operators such as =, <, or <> to
test for NULL. Because the result of any arithmetic comparison with
NULL is also NULL, you cannot obtain any meaningful results from such
comparisons
这就是您体验到的结果。
例子:
mysql> SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL,
> 1 is null, null is null, null = null;
+----------+-----------+----------+----------+-----------+--------------+-------------+
| 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL | 1 is null | null is null | null = null |
+----------+-----------+----------+----------+-----------+--------------+-------------+
| NULL | NULL | NULL | NULL | 0 | 1 | NULL |
+----------+-----------+----------+----------+-----------+--------------+-------------+
我认为这同样适用于所有其他数据库平台。
嗨托尼,
好问题
您正在检查空列。基本上 null 在简单的英语中什么都不是或为空。您不能对 select 空值使用运算符。运算符只能用于匹配具有值且不为空的列。在这种情况下,为空 函数将被使用
我在 sql 服务器上只有一个月的学习经验,我只是想知道为什么之前的第一个查询会产生正确的结果(即连接两个 table 而只有 select从左边 table 开始没有匹配行的行),而第二个查询 returns 是一个空查询。
第一个
select R.Name, A.Name
from tblResident as R
left join tblApartment as A
on R.ApartmentID = A.ID
where R.ApartmentID is null
第二个
select R.Name, A.Name
from tblResident as R
left join tblApartment as A
on R.ApartmentID = A.ID
where R.ApartmentID = null
Table结构
对于 MySQL:
根据MySQL Documentation on Working with NULL Values:
You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL. Because the result of any arithmetic comparison with NULL is also NULL, you cannot obtain any meaningful results from such comparisons
这就是您体验到的结果。
例子:
mysql> SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL,
> 1 is null, null is null, null = null;
+----------+-----------+----------+----------+-----------+--------------+-------------+
| 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL | 1 is null | null is null | null = null |
+----------+-----------+----------+----------+-----------+--------------+-------------+
| NULL | NULL | NULL | NULL | 0 | 1 | NULL |
+----------+-----------+----------+----------+-----------+--------------+-------------+
我认为这同样适用于所有其他数据库平台。
嗨托尼,
好问题
您正在检查空列。基本上 null 在简单的英语中什么都不是或为空。您不能对 select 空值使用运算符。运算符只能用于匹配具有值且不为空的列。在这种情况下,为空 函数将被使用