基于内连接从table中删除记录

Delete Records from table on the basis of inner join

我在 Oracle DB 中有两个 table,分别是 collectioncollection_h。我必须删除 collection_h 中的所有记录,这些记录在 集合 table.[= 中具有相同的以下字段12=]

我必须删除 collection_h table 中因以下查询而出现的所有记录:

select * from collection inner join collection_h on
collection.pos_protocol_id  = collection_h.pos_protocol_id and
collection.terminal_pos_number = collection_h.terminal_pos_number and
collection.cb_file_number = collection_h.cb_file_number and
collection.cb_block_number = collection_h.cb_block_number and
collection.is_stan_batch = collection_h.is_stan_batch and
collection.is_transaction_date = collection_h.is_transaction_date and
collection.is_stan_trans = collection_h.is_stan_trans;

删除存在的地方

delete
from collection as c
where exists (
  select 1
  from collection_h as h
  where h.pos_protocol_id  = c.pos_protocol_id
    and h.terminal_pos_number = c.terminal_pos_number 
    and h.cb_file_number = c.cb_file_number 
    and h.cb_block_number = c.cb_block_number 
    and h.is_stan_batch = c.is_stan_batch  
    and h.is_transaction_date = c.is_transaction_date
    and h.is_stan_trans = c.is_stan_trans
);

db<>fiddle here

上的简化测试

但是如果列在匹配行中可以有 NULL

delete
from collection as c
where exists (
  select 1
  from collection_h as h
  where decode(h.pos_protocol_id, c.pos_protocol_id, 0, 1) = 0
    and decode(h.terminal_pos_number, c.terminal_pos_number, 0, 1) = 0 
    and decode(h.cb_file_number, c.cb_file_number, 0, 1) = 0
    and decode(h.cb_block_number, c.cb_block_number, 0, 1) = 0
    and decode(h.is_stan_batch, c.is_stan_batch, 0, 1) = 0
    and decode(h.is_transaction_date, c.is_transaction_date, 0, 1) = 0
    and decode(h.is_stan_trans, c.is_stan_trans, 0, 1) = 0
);
Delete x from collection X inner join collection_h Y on
X.pos_protocol_id  = Y.pos_protocol_id and
X.terminal_pos_number = Y.terminal_pos_number and
X.cb_file_number = Y.cb_file_number and
X.cb_block_number = Y.cb_block_number and
X.is_stan_batch = Y.is_stan_batch and
X.is_transaction_date = Y.is_transaction_date and
X.is_stan_trans = Y.is_stan_trans;