[英]Using Oracle, how to view non matching results?
I have two oracle tables with similar structures, the main difference is that one is a subset of the other. 我有两个结构相似的oracle表,主要区别是一个是另一个的子集。 What I want to do is perform a join that will return all rows that are not in the subset table. 我想要做的是执行一个联接,该联接将返回不在子表中的所有行。
The issue is that the unique identifier is about 6-7 columns. 问题是唯一标识符约为6-7列。 So I can not just do a WHERE NOT IN
clause. 因此,我不能只执行WHERE NOT IN
子句。 Does anyone know which type of join or how I can do this? 有人知道哪种类型的联接,或者我该怎么做?
Assuming your primary key is (COL1,COL2,...)
: 假设您的主键是(COL1,COL2,...)
:
The easy answer is to use the MINUS
set operator : 简单的答案是使用MINUS
set运算符 :
SELECT COL1,COL2 FROM T
MINUS SELECT COL1,COL2 FROM U;
If you feel the need, or have more complex logic, you might prefer using an outer join : 如果您觉得有需要,或者有更复杂的逻辑,则可能更喜欢使用外部联接 :
SELECT COL1,COL2
FROM T LEFT JOIN U
USING(COL1,COL2)
WHERE U.ROWID IS NULL;
Please notice the WHERE
clause that filters out the result set to only keep PK of rows not present in the U
table. 请注意WHERE
子句过滤掉结果集,以仅保留U
表中不存在的行的PK。
In addition, all comparisons are only performed on the primary key , not checking the other columns. 此外,所有比较仅在主键上执行,而不检查其他列。 See http://sqlfiddle.com/#!4/6e42f/13 . 参见http://sqlfiddle.com/#!4/6e42f/13 。 According to execution plan, you will see that in my very simple example, the OUTER JOIN
performs better. 根据执行计划,您将看到在我的非常简单的示例中, OUTER JOIN
性能更好。
That being said, the subquery syntax might perform even better, as the Oracle optimizer build almost the same execution plan than for the outer join. 话虽这么说, 子查询语法的性能可能甚至更好,因为Oracle优化器构建的执行计划几乎与外部联接的执行计划相同。 But , the key difference is the use of an anti nested loop , removing the need for the extra NOT NULL
filter: 但是 ,关键区别在于使用了反嵌套循环 ,从而无需使用额外的NOT NULL
过滤器:
SELECT COL1,COL2 FROM T
WHERE (COL1,COL2) NOT IN (SELECT COL1,COL2 FROM U);
As far as I know, the NOT IN
operator is the only way to perform the anti join you need. 据我所知, NOT IN
运算符是执行所需的反联接的唯一方法。
You can write a MINUS
query. 您可以编写一个MINUS
查询。 Make sure you mention exact column
names, ie exact number of columns with same data type
in both SQLs in the MINUS query. 确保在MINUS查询的两个SQL中都提到确切的column
名,即具有相同data type
确切column
数。
The result set thus returned is the difference in the rows between the two tables. 这样返回的结果集是两个表之间的行之差。
where not in
is the answer. 答案where not in
。
the query will be something like this: 查询将是这样的:
select blam, hoot, kapow
from schmarr
where blam not in
( select blam from smashy )
The column blam
must have the following properties: 列blam
必须具有以下属性:
the schmarr table is the superset table. schmarr表是超集表。 the smashy table is the subset table. smashy表是子表。
let's assume Table-B is subset of Table-A. 我们假设表B是表A的子集。
select * from Table-A left outer join Table-b on
Table-A.column1 = Table-b.column2 and
Table-A.column2 = Table-b.column2 and ....
where the columns1,column2,... are the unique identifiers 其中column1,column2,...是唯一标识符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.