三个table用一个Primary Key合并,每个table的结果应该一一显示

Merging three tables with one Primary Key and the result of each table should be displayed one by one

我有3个table,他们有一个共同领域。我想 select 每个 table 的所有行,按该公共字段排序,结果应该是因为第一行来自第一个 table,第二行来自第二个 table,第3行来自第3table。

哪种查询适用于上述任务?给出了三个 table 的示例数据和所需的输出如下。如果我的问题很笨拙,请原谅!!

TABLE1      
WO      STATUS      SITE
1001    Released    36
1002    Closed      31
1003    Released    42

TABLE2          
WO  LINE NO   PRICE     QTY
1001    1     100        2
1001    2     300        3
1002    1     1500       3
1003    1     100        4
1003    2     200        2
1003    3     100        1

TABLE3      
WO      USER    SIGN ID
1001    Chrish  CRS
1001    Jovan   JVN
1002    Roopesh ROO
1003    Brian   BRN
1003    Suren   SRN
1003    Pavith  PAV

并且输出应该像

RESULT              
TABLE1  1001    Released    36  
TABLE2  1001    1   100     2
TABLE2  1001    2   300     3
TABLE3  1001    Chrish      CRS 
TABLE3  1001    Jovan       JVN 
TABLE1  1002    Closed      31  
TABLE2  1002    1   1500    3
TABLE3  1002    Roopesh     ROO 
TABLE1  1003    Released    42  
TABLE2  1003    1   100     4
TABLE2  1003    2   200     2
TABLE2  1003    3   100     1
TABLE3  1003    Brian       BRN 
TABLE3  1003    Suren       SRN 
TABLE3  1003    Pavith      PAV 
select commonfield, fld1, fld2 from (
    select commonfield, uniquefield1 as fld1, uniquefield2 as fld2, 1 as sort from table1
    union
    select commonfield, uniquefield1, uniquefield2, 2 as sort from table2
    union
    select commonfield, uniquefield1, uniquefield2, 3 as sort from table3
) as qry
order by qry.commonfield, qry.sort

SQL DEMO

SELECT 'T1' as source, "WO", "STATUS" as f2, CAST("SITE" AS text)  as f3, null as f4
FROM Table1
UNION
SELECT 'T2' as source, "WO", CAST("LINE NO" AS text) as f2, CAST("PRICE" AS text) as f3, "QTY" as f4
FROM Table2
UNION
SELECT 'T3' as source, "WO", "USER" as f2,  "SIGN ID" as f3, null as f4
FROM Table3 
ORDER BY "WO", source

输出