2017-10-19 110 views
0

我有3個SELECT FROM,我想在一個表中合併3個結果,我不能使用一個select * from和使用JOIN作爲3個表。從多個表中合併列,不按順序排列

我可以合併使用A和B列作爲密鑰的表。 我需要在3個合併表上的列不是以相同的順序進行最後的UNION。

例子:

SELECT A, B, C, D, E, F, G, H, I 
FROM Table 
UNION ALL 
(
    SELECT A, B, F, G FROM Stuff 
    --JOIN?MERGE?WhatHere? 
    SELECT A, B, E, C FROM otherStuff 
    --JOIN?MERGE?WhatHere? 
    SELECT A, B, D, H FROM otherstuffbis 
    I --adding null value to columns 
) 
+0

'MySQL'或'SQL-Server'?選一個。 – waka

+0

對不起,自動完成,sql server –

回答

1

試試這個

SELECT A, B, C, D, E, F, G, H, I 
FROM Table 
UNION ALL 
(


    SELECT 
    A, 
    B, 
    NULL AS c, 
    NULL AS D, 
    NULL AS E, 
    F, 
    G , 
    NULL AS H, 
    NULL AS I 
    FROM [stuff] 


    SELECT 
    A, 
    B, 
    c, 
    NULL AS D, 
    E, 
    NULL AS F, 
    NULL AS G , 
    NULL AS H, 
    NULL AS I 
    FROM [otherStuff] 

    UNION ALL 

    SELECT 
    A, 
    B, 
    NULL AS c, 
    NULL AS D, 
    E, 
    NULL AS F, 
    NULL AS G , 
    H, 
    NULL AS I 
    FROM [otherstuffbis] 


) A