2017-09-17 46 views
-3

我有這樣的代碼:加入4桌與指定列

SELECT A.UNITCODE, B.FORMATIONCODE, C.UPPERFORMATIONCODE, D.UPPERFORMATIONCODE 
FROM UNIT AS A.UNITCODE 
INNER JOIN FORMATION AS B.FORMATIONCODE 
INNER JOIN UPPERFORMATION_UNIT AS C.UPPERFORMATION 
INNER JOIN UPPERFORMATION AS D.UPPERFORMATIONCODE 
WHERE UNITCODE='7000007' 

你們能幫助我如何加入4桌與指定列?

+1

顯示這些表是如何相關的......哪些列值必須用於連接? – scaisEdge

+1

對於加入,您需要顯示所有4個表格的關係。 –

回答

1

Assuiming所有3代相關的表具有相同的UNIT_ID與表單位

SELECT 
     A.UNITCODE 
     , B.FORMATIONCODE 
     , C.UPPERFORMATIONCODE 
     , D.UPPERFORMATIONCODE 
    FROM UNIT AS A 
    INNER JOIN FORMATION AS B ON B.FORMATIONCODE = A.UNIT_ID 
    INNER JOIN UPPERFORMATION_UNIT AS C. C.UPPERFORMATION = A.UNIT_ID 
    INNER JOIN UPPERFORMATION AS D D.UPPERFORMATIONCODE = A.UNIT_ID 
    WHERE UNITCODE='7000007' 
+0

謝謝....幫助很多 –

+0

@IrwanAhmad好,如果我的回答是正確的,請將其標記爲已接受...看到這裏如何 http://meta.stackexchange.com/questions/5234/how-does-accepting-的回答工作 – scaisEdge

0

看來你是混亂的表的別名和鏈接欄加入。

這是如何給表的別名在查詢中,以增強可讀性:

INNER JOIN formation AS f 

其中AS是可選的。大多數情況下它是沒有意義的。

這是如何加入:

FROM unit AS u 
INNER JOIN upperformation_unit AS ufu ON ufu.unitcode = u.unitcode 

好了,我不知道聯課程表中的列,更不用說他們的名字。但我想這個查詢將不得不看起來像這樣或多或少:

SELECT 
    u.unitcode, 
    f.formationcode, 
    uf.upperformationcode, 
    ufu.upperformationcode 
FROM unit u 
JOIN upperformation_unit ufu ON ufu.unitcode = u.unitcode 
JOIN upperformation uf ON uf.upperformationcode = ufu.upperformationcode 
JOIN formation f ON f.formationcode uf.formationcode 
WHERE u.unitcode = 7000007;