· 6 years ago · May 06, 2019, 08:10 PM
1do
2$$
3begin
4create table table5(id int, s3 varchar(20));
5if exists (select ID, S3 from table1 join table2 on table2.S1ID=table1.S1)
6 then insert into table5 (select ID, S3 from table1 join table2 on table2.S1ID=table1.S1);
7end if;
8if exists (with res as (select ID, S1, S3 from table1 join table3 on table3.S1ID=table1.S1)
9select ID, S1, S3 from res where res.S1 not in (select S1 from res join table2 on table2.S1Id=res.S1))
10 then insert into table5 (with res as (select ID, S1, S3 from table1 join table3 on table3.S1ID=table1.S1)
11 select ID, S3 from res where res.S1 not in (select S1 from res join table2 on table2.S1Id=res.S1));
12end if;
13end;
14$$;
15
16select * from table5;