删除表1中不存在的Table1中的数据

时间:2017-10-27 20:34:15

标签: oracle

我有2个不同的表,我想删除table1中不存在于Tables2

中的记录

表1:

select col1 from Table1

表2:

select 
    concat('A_',col1) 
from 
    Table2 
where 
    Col2 = '748' 
    and Col3 = 'D' 
    and Col4 = 'Account'

现在我想删除Table1的差异......

2 个答案:

答案 0 :(得分:0)

可以使用minus操作和insert into语句完成此操作。

insert into table3(col) (

  select col1 from Table1

  minus 

  select 
      concat('A_',col1) 
  from 
      Table2 
  where 
      Col2 = '748' 
      and Col3 = 'D' 
      and Col4 = 'Account'
)

然后可以使用delete语句(例如

)从table1中删除记录
delete from table1
where col1 in (
  select col1 from Table1

  minus 

  select 
      concat('A_',col1) 
  from 
      Table2 
  where 
      Col2 = '748' 
      and Col3 = 'D' 
      and Col4 = 'Account'
)

答案 1 :(得分:0)

delete from table1 t1
where  not exists ( select * from table2 where  col2 || col3 || col4 = t1.col1 );

除以下情况外,这将起作用;在这种情况下你需要解释你想要的东西。可以修改DELETE语句以适应。

如果t1.col1NULL,则将被删除,即使 table2中的行col2col3并且col4都是NULL。这种情况是否可能(t1.col1中的col2, col3, col4table2 全部 NULL?在这种情况下,t1中的行应该是保留而不是删除?