[JAVA] When performing a full outer join without using a full outer join

If you want to perform a full outer join, you can issue a query with "full outer join" as the keyword. Often not supported by some DB servers.

In such a case, use "union all" to perform a complete outer join. An example is shown in the query below.

select distinct * from (select e1.seirino,e1.time,e2.buyer_no,e2.buyer_name from daityou e1 inner join a0160 e2 on e1.seirino = e2.seirino where e1.time ='2018-10-27 00:00:00.0')   j1 left join (select distinct a1.seirino,a1.buyer_no,a1.a0162_no,a1.A0162_NAME,a1.a0164_no ,a1.a0164_name,a2.a0168_no ,a2.a0168_name

from (select distinct q1.seirino,q1.buyer_no,q1.a0162_no,q1.A0162_NAME,q2.a0164_no ,q2.a0164_name       from A0162 q1 left join  A0164  q2 on q1.seirino=q2.seirino and q1.buyer_no=q2.buyer_no and q1.a0162_no=q2.a0164_no      union all      select  q2.seirino,q2.buyer_no,null as a0164_no,null as A0164_NAME,q2.a0164_no ,q2.a0164_name      from A0164 q2        where not exists(select * from A0162 q1      where q2.seiriNO = q1.seiriNO and q1.buyer_no=q2.buyer_no and q1.a0162_no=q2.a0164_no   ) )

a1 left join A0168 a2 on a1.seirino = a2.Untitled.png seirino and a1.buyer_no=a2.buyer_no and (a1.a0162_no=a2.a0168_no or a1.a0164_no=a2.a0168_no)    union all

select  d2.seirino,d2.buyer_no,null as a0162_no,null as A0162_NAME,null as a0164_no,null as A0164_NAME,d2.a0168_no ,d2.a0168_name      from A0168 d2       where not exists(select * from     (select distinct c1.seirino,c1.buyer_no,c1.a0162_no,c1.A0162_NAME,c2.a0164_no ,c2.a0164_name          from A0162 c1 left join  A0164  c2 on c1.seirino=c2.seirino and c1.buyer_no=c2.buyer_no and c1.a0162_no=c2.a0164_no    union all      select  c2.seirino,c2.buyer_no,null as a0164_no,null as A0164_NAME,c2.a0164_no ,c2.a0164_name       from A0164 c2        where not exists   (select * from A0162 c1  where c2.seiriNO = c1.seiriNO and c1.buyer_no=c2.buyer_no and c1.a0162_no=c2.a0164_no   ) ) d1     where d2.seiriNO = d1.seiriNO and d1.buyer_no=d2.buyer_no and (d1.a0162_no=d2.a0168_no or d1.a0164_no=d2.a0168_no))) j2 on j1.seirino=j2.seirino and j1.buyer_no=j2.buyer_no

order by j1.seirino,j1.buyer_no ,j2.a0162_no ASC NULLS LAST,j2.a0164_no ASC NULLS LAST,j2.a0168_no ASC NULLS LAST;

◆ Table state

SEIRINO TIME BUYER_NO BUYER_NAME SEIRINO BUYER_NO A0162_NO A0162_NAME A0164_NO A0164_NAME A0168_NO A0168_NAME
1 2018-10-27 00:00:00.0 1 A 1 1 1 N1 1 A4 1 1 1 2018-10-27 00:00:00.0 1 A 1 1 2 N2 2 B4 2 2 1 2018-10-27 00:00:00.0 1 A 1 1 3 N3 3 C4 3 3 1 2018-10-27 00:00:00.0 1 A 1 1 4 N4 4 4 4 4 1 2018-10-27 00:00:00.0 1 A 1 1 null null 5 5 5 5 1 2018-10-27 00:00:00.0 1 A 1 1 null null null null 6 6 1 2018-10-27 00:00:00.0 2 B 1 2 1 M1 1 D4 1 1 1 2018-10-27 00:00:00.0 2 B 1 2 2 M2 2 D4 null null 1 2018-10-27 00:00:00.0 3 C 1 3 1 L1 1 D4 null null 1 2018-10-27 00:00:00.0 4 D null null null null null null null null

that's all.

Recommended Posts

When performing a full outer join without using a full outer join
How to join a table without using DBFlute and sql
Realize a decision table without using conditional branching
[Android] Create a sliding menu without using NavigationView
A memo when fumbling and developing using JavaFX
A memorandum when trying to create a GUI using JavaFX
Find the remainder divided by 3 without using a number