加入表格复杂

时间:2012-04-18 09:59:24

标签: sql join left-join

我有这些表

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 

提前谢谢你, 甜菊

2 个答案:

答案 0 :(得分: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%正确但只是加入关系表:)

答案 1 :(得分: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