简体   繁体   English

Oracle筛选器查询不起作用

[英]Oracle filter query not working

I have a query as follows- 我有一个查询如下-

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

pid column from table 1 has some null values (say 10 records with pid = null). 表1中的pid列具有一些空值(例如pid = null的10条记录)。 I want to modify above query as it should return these null value rows as well. 我想修改上面的查询,因为它也应该返回这些空值行。 Like all the 100 records from above query plus 10 records having pid = null. 像上述查询中的所有100条记录以及pid = null. 10条记录一样pid = null.

I tried 我试过了

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

But this query is returning more rows. 但是此查询返回更多行。

I want exact 110 rows. 我想要确切的110行。 Can anybody tell me what I am doing wrong here? 有人可以告诉我我在做什么错吗?

ANSWER 回答

By using all tricks in following answer. 通过使用以下所有技巧。 Here is the final query this may help others- 这是最终的查询,可能对其他人有帮助-

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

First, use proper, explicit JOIN syntax. 首先,使用正确的显式JOIN语法。 Simple rule: never use commas in the FROM clause. 简单规则:请勿在FROM子句中使用逗号。

Your problem could be fixed with parentheses. 您的问题可以用括号解决。 But it is better to use proper syntax: 但是最好使用适当的语法:

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

I see. 我懂了。 The question is clearer now. 现在的问题更清楚了。 You seem to want a left join : 您似乎想要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);

This returns all rows in table1 that match the where conditions, even if they do not match the condition in table2 . 这将返回table1中与where条件匹配的所有行,即使它们与table2中的条件不匹配也是如此。

Operator precedence makes your query like this 运算符优先级使您的查询像这样

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

Use parentheses or proper JOIN syntax. 使用括号或正确的JOIN语法。

Adding to the answer of Gordon, you need OR condition 除了戈登的答案,您还需要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;

Try this, 尝试这个,

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

You can not compare null values on the join, please investigate about left, right, outer join, previous query indicates that you want all the records that are in both tables plus those one on table2 that has no correspondency with table1. 您无法比较联接上的空值,请调查左联接,右联接,外联接,上一个查询表明您想要两个表中的所有记录以及table2上与table1不对应的记录。 Also check the next. 还要检查下一个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM