加入MYSQL中的第三个表

时间:2014-01-16 15:15:47

标签: mysql sql

我有三张桌子需要加入,我已经能够加入其中两张:

SELECT 
  coll.title AS CollectionTitle, 
  cont.CollectionID, 
  cont.title AS ContainerTitle, 
  cont.ID as ContainerID, 
  cont.LevelContainerID 
FROM tblCollections_Content cont 
JOIN  tblCollections_Collections coll 
  ON cont.collectionid = coll.id 
WHERE cont.title is NOT NULL 
ORDER BY CollectionID, ContainerID

但是,我还需要加入此表tblCollections_UserFields,并需要选择以下字段:ContentID, Title, Value, EADElementID;此时加入此表中的ContentIDcont.collectionid=coll.id(这是前两个表连接的位置)。

2 个答案:

答案 0 :(得分:0)

只需添加JOIN anotherTable ON ...

即可

答案 1 :(得分:0)

SELECT 
  coll.title AS CollectionTitle, 
  cont.CollectionID, 
  cont.title AS ContainerTitle, 
  cont.ID as ContainerID, 
  cont.LevelContainerID,
  cont.ContentID,
  user.Title,
  user.Value,
  user.EADElementID 
FROM tblCollections_Content cont 
JOIN  tblCollections_Collections coll 
  ON cont.collectionid = coll.id 
JOIN tblCollections_UserFields user
  ON cont.ContentId = user.ContentId
WHERE cont.title is NOT NULL 
ORDER BY CollectionID, ContainerID

我必须假设您在tblCollections_Content中有一个ContentID列以加入您的tblCollections_UserFields,并且您指定的列都在tblCollections_UserFields表中。

根据这些表的引用完整性,您可能需要使用外部联接来查看所有行。