是否可以将联合和外部联接在一起?

时间:2018-02-04 04:00:20

标签: sql oracle union outer-join

所以我的问题是' ON'如果人们从源表中删除,则子句将不符合删除条件。有没有办法在联合上进行外部联接以包括是否有任何内容被删除以使删除部分起作用?这件事将持续5分钟。对不起,如果这是我尝试做的一个不好的例子。

merge into table_one ab
using ( select people,job from table_jobs
where job = 'police'
union
select b.people,b.job from second_table_jobs b
where b.job = 'firefighter') zba
on (ab.people = zba.people)
update 
set 
ab.job ='firefighter'
where b.people = ab.people
and zba.job <> ab.job
delete where zba.people <> ab.people
when not matched then
insert 
(
people,
job
)
values
(
zba.people,
zba.job
)

1 个答案:

答案 0 :(得分:0)

您可以使用DELETE运行单独的NOT EXISTS语句,以保持其清洁。

DELETE FROM table_one ab
WHERE  NOT EXISTS (SELECT 1
                   FROM   (SELECT people,
                                  job
                           FROM   table_jobs
                           WHERE  job = 'police'
                           UNION
                           SELECT b.people,
                                  b.job
                           FROM   second_table_jobs b
                           WHERE  b.job = 'firefighter') zba
                   WHERE  ab.people = zba.people);