2014-01-16 57 views
-5

我有我需要加入三个表,我已经能够加入他们两个这样的:加入第三个表在MYSQL

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(这是前两个表连接的位置)。

+4

那么加入第三个表格时出现了什么问题? –

+0

与第二张表相同... –

回答

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加入到你的tblCollections_UserFields具有内容识别列,您指定的列都在tblCollections_UserFields表。

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

0

只需添加JOIN anotherTable ON ...