【发布时间】:2018-03-14 15:21:57
【问题描述】:
所以,我有一个简单的 SQL 查询,但它似乎有问题(或者我的 where-clause 写错了),因为如果我在特定字段 (matflag) 上选择它不会返回值一个特定的值 (50)。
查询基本上是一个select from table1,在table2 上有一个子查询,其中where-clause 只是检查子查询的返回字段是否存在于table1:
Select distinct
t1.matnum as matnum, t1.matflag as matflag, t1.factory as factory
from
table1 t1,
(select matnum from table2 where technical_value = 'XX') t2
where
t1.matnum = t2.matnum and t1.matnum = '60000000';
这会返回这个输出:
+----------+---------+---------+
| MATNUM | MATFLAG | FACTORY |
+----------+---------+---------+
| 60000000 | | 001000 |
| 60000000 | | 002000 |
| 60000000 | | 003000 |
| 60000000 | | 004000 |
| 60000000 | | 005000 |
+----------+---------+---------+
如果我将and t1.matflag != '50' 添加到where-clause 的末尾,整个输出就会消失。
Select distinct
t1.matnum as matnum, t1.matflag as matflag, t1.factory as factory
from
table1 t1,
(select matnum from table2 where technical_value = 'XX') t2
where
t1.matnum = t2.matnum
and t1.matnum = '60000000'
and t1.matflag != '50';
输出:
+----------+---------+---------+
| MATNUM | MATFLAG | FACTORY |
+----------+---------+---------+
matflag 列的附加信息:它是一个 varchar2(2 Char) 列,要么填充任何内容,要么填充值“50”或值“10”或“20”。
现在,如果我将 where 子句从 and t1.matflag != '50' 更改为 and t1.matflag is null,则输出再次正确:
Select distinct
t1.matnum as matnum, t1.matflag as matflag, t1.factory as factory
from
table1 t1,
(select matnum from table2 where technical_value = 'XX') t2
where
t1.matnum = t2.matnum
and t1.matnum = '60000000'
and t1.matflag is null;
所以这会返回这个输出:
+----------+---------+---------+
| MATNUM | MATFLAG | FACTORY |
+----------+---------+---------+
| 60000000 | | 001000 |
+----------+---------+---------+
.... and so on, have a look at the first table above
如果我选择is null,但如果我选择!= '50',它会如何返回? (旁注:将!= 更改为<> 也无济于事)
matflag is null 如何适用但matflag != '50' 不适用?
我们运行 Oracle Database 11g 版本 11.2.0.3.0 - 64 位生产。
【问题讨论】:
-
在 SQL 中
NULL表示“未知”值,因此对其进行操作将得到NULL,即BOOLEAN化为FALSE....