Sql Oracle筛选器查询不工作

Sql Oracle筛选器查询不工作,sql,oracle,where,Sql,Oracle,Where,我有一个问题如下- Select t1.colmn1, t2.colm1 from table1 t1, table2 t2 where t1.pid = t2.pid and t1.colm2 is null; --- This query retuns 100 rows 表1中的pid列有一些空值(比如10条pid=null的记录)。 我想修改上面的查询,因为它也应该返回这些空值行。 与上面查询中的所有100条记录加上10条具有pid=null的记录一样。 我试过了 Select colm

我有一个问题如下-

Select t1.colmn1, t2.colm1 from table1 t1, table2 t2
where t1.pid = t2.pid
and t1.colm2 is null; --- This query retuns 100 rows
表1中的pid列有一些空值(比如10条pid=null的记录)。 我想修改上面的查询,因为它也应该返回这些空值行。 与上面查询中的所有100条记录加上10条具有
pid=null的记录一样。

我试过了

Select colmn1 from table1 t1, table2 t2
where t1.pid = t2.pid or t1.pid is null
and t1.colm2 is null;
但是这个查询返回了更多的行

我要整整110排。有人能告诉我我做错了什么吗

回答

通过使用以下答案中的所有技巧。这是最后一个问题,这可能会帮助其他人-

Select colmn1
from table1 t1 left join
     table2 t2
     on t1.pid = t2.pid
where (t1.colm2 is null) or (t2.pid is not null)
and t1.colm2 is null

首先,使用正确、明确的
JOIN
语法。简单规则:切勿在
FROM
子句中使用逗号

你的问题可以用括号解决。但最好使用正确的语法:

Select colmn1
from table1 t1 join
     table2 t2
     on t1.pid = t2.pid
where t1.pid is null and t1.colm2 is null;
我明白了。现在问题更清楚了。您似乎想要一个
左连接

Select colmn1
from table1 t1 left join
     table2 t2
     on t1.pid = t2.pid
where (t1.colm2 is null) or (t2.pid is not null);

这将返回
table1
中与
where
条件匹配的所有行,即使它们与
table2
中的条件不匹配

...or( t1.pid is null and t1.colm2 is null;)

使用括号或正确的连接语法。

在Gordon的答案中添加,您需要或条件

Select colmn1
from table1 t1 inner join
     table2 t2
     on t1.pid = t2.pid
where t1.pid is null
or t1.colm2 is null;
试试这个

选择colmn1
来自表1 t1、表2 t2
其中t1.pid=t2.pid(+)
t1.colm2为空

您不能比较联接上的空值,请调查左、右、外联接,上一个查询表明您需要两个表中的所有记录加上表2中与表1不对应的记录。 还要检查下一个

选择colmn1
来自表1 t1、表2 t2
其中t1.pid(+)=t2.pid

t1.colm2为空

这不会给出期望的结果,因为我们正在where条件中添加t1.pid=null。@MicrosoftDN。我现在明白了。已编辑的查询应该可以执行您想要的操作。带有左联接的已编辑答案中的第一个查询仅返回10行(带空值),另一个查询的结果与我得到的结果相同。@MicrosoftDN。我的误解。我以为您只需要
NULL
值。
where
子句是不必要的。
t1.colm2为null
是必需的。我只想要所有符合
t1.pid=t2.pid
条件的行,以及
t1.pid为空的行,请提供示例数据和所需结果。