所以,我有一个简单的SQL查询,但似乎有问题(或者我的where-clause
写错了),因为如果我选择特定的字段,它就不会返回值({ {1}})具有特定值(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 |
+----------+---------+---------+
的附加信息:它是一个varchar2(2个字符)列,要么没有填充,要么填充值' 50'或者价值' 10'或者' 20'。
现在,如果我将where子句从matflag
更改为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
不适用?
我们运行Oracle Database 11g 11.2.0.3.0版 - 64位生产。
答案 0 :(得分:3)
了解如何使用正确的显式JOIN
语法:
Select distinct t1.matnum as matnum, t1.matflag as matflag, t1.factory as factory
from table1 t1 join
table2
on t1.matnum = t2.matnum
where t2.technical_value = 'XX' and t1.matnum = '60000000';
然后了解NULL
值及其几乎每次比较失败的方法,包括<>
。
你想要的逻辑是:
where t2.technical_value = 'XX' and t1.matnum = '60000000' and
(matflag <> '50' or matflag is null)
答案 1 :(得分:1)
在SQL NULL
中表示“未知”值,因此使用它的任何操作都将导致NULL
。试试COALESCE(t1.matflag, 0) <> 50
...