2012-04-18 69 views
-2

我有這些表加入表複雜

Person 
ID_PRS Name 
------ ----- 
1  John 
2  Brian 
3  Mike 

Con_PRS_Fonc 
    ID_PRS ID_FONCT 
    ----- ------- 
    1  17 
    1  18 
    3  17 

    Fonction 
     ID_FONCT LABEL ID_CATEG ID_MET ID_ANA 
     ------- ----- -------- ------ ------ 
     17  Boss  1  56  78 
     18  Junior 1  45  56 

Category 
ID_CATEG LABEL 
-------- ----- 
    1  Cleaning 
    2  Wahsing 

MEtir 
ID_MET LABEL 
------ ----- 
    56  Go 
    45  Come 

Analys 
ID_ANA LABEL 
------ ----- 
    78  Think 
    56  Drink 

我怎樣才能得到:

ID_PRS NOM ID_FONCT LABEL_FONCT ID_CATEG LAB_CAT ID_MET LAB_MET ID_AN LAB_ANA 
------ ---- -------- ----------- -------- ------- ------ ------- ----- ------- 
    1 John 17   Boss   1  Cleaning 56  GO  56  Think 

我只知道我可以加入與表Con_PRS_Fonc表的人,但我怎麼能與其他參加?

SELECT * FROM PERSON 
LEFT JOIN Con_PRS_Fonc ON PERSON.ID_PRS = Con_PRS_Fonc .ID_PRS 

感謝你在前進, 甜菊

+0

建議:使用在整個模型中唯一的有意義的名稱重命名多個「LABEL」屬性,例如'Category_label','MEtir_label'和'Analys_label'分別。 – onedaywhen 2012-04-18 13:30:17

+0

...然後你的查詢將會非常簡單:'SELECT * FROM person NATURAL JOIN Con_PRS_Fonc NATURAL JOIN FONCTION NATURAL JOIN CATEGORY NATURAL JOIN METIr NATURAL JOIN Analys;' – onedaywhen 2012-04-18 13:32:36

回答

0

這應該正是你想要的:

SELECT 
Person.ID_PRS, 
Person.Name, 
Fonction.ID_FONCT, 
Fonction.LABEL AS LABEL_FONCT, 
Fonction.ID_CATEG, 
Category.LABEL AS LAB_CAT, 
MEtir.ID_MET, 
MEtirLABEL AS LAB_MET, 
Analys.ID_ANA, 
Analys.LABEL AS LAB_ANS 
FROM PERSON 
INNER JOIN Con_Prs_Fonc ON Person.ID_PRS = Con_Prs_Fonc.ID_PRS 
INNER JOIN Fonction ON Fonction.ID_FONCT = Con_Prs_Fonc.ID_FONCT 
INNER JOIN Category ON Category.ID_CATEG = Fonction.ID_CATEG 
INNER JOIN MEtir ON MEtir.ID_MET = Fonction.ID_MET 
INNER JOIN Analys ON Analys.ID_ANA = Fonction.ID_ANA 
0

事情是這樣的:

SELECT * FROM person as P 
INNER JOIN Con_PRS_Fonc AS CPF ON P.ID_PRS = CPF.ID_PRS 
INNER JOIN FONCTION AS FON ON FON.ID_FONCT = CPF.ID_FONCT 
INNER JOIN CATEGORY AS C ON FON.ID_CATEG = C.ID_CATEG 
INNER JOIN MEtir AS MEt ON Fon.ID_MET = MEt.ID_MET 
INNER JOIN Analys AS ANA ON Fonc.ID_ANA = ANA.ID_ANNA 

我無法證實其100%正確的,但只是參加表格上的關係:)