简体   繁体   English

oracle找到2个表之间的差异

[英]oracle find difference between 2 tables

I have 2 tables that are the same structure. 我有2个相同结构的表。 One is a temp one and the other is a prod one. 一个是临时的,另一个是刺激的。 The entire data set gets loaded each time and sometimes this dataset will have deleted records from the prior datasets. 每次加载整个数据集,有时此数据集将从先前的数据集中删除记录。 I load the dataset into temp table first and if any records were deleted I want to deleted them from the prod table also. 我首先将数据集加载到临时表中,如果删除了任何记录,我也想从prod表中删除它们。

So how can I find the records that exist in prod but not in temp? 那么如何才能找到prod中存在但不存在于temp中的记录? I tried outer join but it doesn't seem to be working. 我试过外连接,但它似乎没有工作。 It's returning all the records from the table in the left or right depending on doing left or right outer join. 它将从左侧或右侧的表中返回所有记录,具体取决于执行左外连接或右外连接。

I then also want to delete those records in the prod table. 然后我还想删除prod表中的那些记录。

One way would be to use the MINUS operator 一种方法是使用MINUS运算符

SELECT * FROM table1
MINUS
SELECT * FROM table2

will show all the rows in table1 that do not have an exact match in table2 (you can obviously specify a smaller column list if you are only interested in determining whether a particular key exists in both tables). 将显示所有行table1没有确切匹配table2 (你可以明显地指定一个较小的列的列表,如果你只确定两个表中是否存在特定的键兴趣)。

Another would be to use a NOT EXISTS 另一个是使用NOT EXISTS

SELECT *
  FROM table1 t1
 WHERE NOT EXISTS( SELECT 1
                     FROM table2 t2
                    WHERE t1.some_key = t2.some_key )

How about something like: 怎么样的:

SELECT * FROM ProdTable WHERE ID NOT IN
   (select ID from TempTable);

It'd work the same as a DELETE statement as well: 它的工作方式与DELETE语句相同:

DELETE FROM ProdTable WHERE ID NOT IN
   (select ID from TempTable);

MINUS can work here The following statement combines results with the MINUS operator, which returns only rows returned by the first query but not by the second: MINUS可以在这里工作以下语句将结果与MINUS运算符组合在一起,后者仅返回第一个查询返回的行,但不返回第二个查询返回的行:

SELECT * FROM prod
MINUS
SELECT * FROM temp;

Minus will only work if the table structure is same 减号仅在表结构相同时才有效

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

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