简体   繁体   English

MySQL加入3个级联表

[英]MySQL Join 3 cascading tables

I have seen plenty of examples to join multiple tables back to one master table but this query is different in that it cascades. 我已经看到很多例子将多个表连接回一个主表,但是这个查询的不同之处在于它级联。

I have 3 tables similar to the below (simplified for ease): 我有3个类似于下面的表(为了方便简化):

**CONTACTS**     
--------------------------------------------
Cid    name
--------------------------------------------
1      John
2      Peter
3      Karl


**OPPORTUNITIES**
--------------------------------------------
Oid   Cidlink   title
--------------------------------------------
1     2         php lookup script
2     2         php facial recognition
3     3         html email template
4     1         javascript verification

**ATTACHMENTS**
--------------------------------------------
Aid    Oidlink  attachment
--------------------------------------------
1     3         received enquiry
2     3         header and footer done
3     3         pixel trace image done on server
4     2         database structure done      
5     2         html get form done

As you can see, attachments link back to opportunities and opportunities relate to contacts. 如您所见,附件链接回与联系人相关的机会和机会。 Attachments do not link directly to contacts except via the opportunity table. 除了通过商机表,附件不会直接链接到联系人。

I need to create a recordset including fields from all 3 tables and am struggling. 我需要创建一个记录集,包括来自所有3个表的字段,我正在努力。

SELECT CONTACTS.*, OPPORTUNITIES.*, ATTACHMENTS.*
FROM ATTACHMENTS
    INNER JOIN OPPORTUNITIES
        ON CONTACTS.Cid = OPPORTUNITIES.Cidlink
    INNER JOIN ATTACHMENTS
        ON OPPORTUNITIES.Oid = ATTACHMENTS.Oidlink

ORDER BY ****whatever****

Am I close or am I approaching this the wrong way? 我是关闭还是我接近这个错误的方式?

I think you mean to have contacts be the first table not attachments... 我认为你的意思是让联系人成为第一张没有附件的表......

SELECT CONTACTS.*, OPPORTUNITIES.*, ATTACHMENTS.*
FROM ATTACHMENTS  --- This is wrong
INNER JOIN OPPORTUNITIES
  ON CONTACTS.Cid = OPPORTUNITIES.Cidlink
INNER JOIN ATTACHMENTS
  ON OPPORTUNITIES.Oid = ATTACHMENTS.Oidlink
ORDER BY ****whatever****

I think you mean (and use aliases easier to read in the long run) 我认为你的意思是(从长远来看,使用别名更容易阅读)

SELECT C.*, O.*, A.*
FROM Contacts C
INNER JOIN OPPORTUNITIES O
   ON C.Cid = O.Cidlink
INNER JOIN ATTACHMENTS A
   ON O.Oid = A.Oidlink
ORDER BY ****whatever****

Since you indicated you need fields from all 3 tables I'm assuming INNER JOIN is appropriate here; 既然你表示你需要来自所有3个表的字段,我假设INNER JOIN在这里是合适的; but maybe you want to use an OUTER JOIN to include all contacts? 但也许你想使用OUTER JOIN来包含所有联系人? Include all contacts and their opportunities if they exist. 包括所有联系人及其存在的机会。 and include all attachments for opportunities if if they exist) 并包括机会的所有附件(如果存在)

SELECT C.*, O.*, A.*
FROM Contacts C
LEFT JOIN OPPORTUNITIES O
   ON C.Cid = O.Cidlink
LEFT JOIN ATTACHMENTS A
   ON O.Oid = A.Oidlink
ORDER BY ****whatever****

For an improved understanding of joins this Venn Diagram approach from CodingHorror is a good start. 为了更好地理解连接,来自CodingHorror的这种维恩图方法是一个良好的开端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM