是否有可能将联合和外部连接在一起?

is it possible to have a union and a outer join together?

所以我这里的问题是如果人们从源表中删除,'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
)

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

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);