Oracle 过滤器查询不起作用

Oracle filter query not working

我有如下疑问-

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
来自 table 1 的

pid 列有一些空值(比如 10 个 pid = null 的记录)。 我想修改上面的查询,因为它也应该 return 这些空值行。 就像上面查询中的所有 100 条记录加上具有 pid = null.

的 10 条记录

我试过了

Select colmn1 from table1 t1, table2 t2
where t1.pid = t2.pid or t1.pid is null
and t1.colm2 is null;

但是这个查询 return 更多的行。

我想要精确的 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;

我明白了。现在问题更清楚了。你似乎想要 left join:

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);

这 returns table1 中符合 where 条件的所有行,即使它们不符合 table2.

中的条件

运算符优先级使您的查询像这样

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

使用括号或正确的 JOIN 语法。

添加到戈登的答案中,您需要 OR 条件

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

试试这个,

Select colmn1 from table1 t1, table2 t2 where t1.pid = t2.pid(+) and t1.colm2 is null;

您无法比较连接上的空值,请调查左、右、外连接,之前的查询表明您想要两个表中的所有记录加上表 2 上与表 1 没有对应关系的记录. 还要检查下。

Select colmn1 from table1 t1, table2 t2 where t1.pid(+) = t2.pid and t1.colm2 is null;