简体   繁体   中英

Join on multiple tables one query

I have 5 tables pages, articles, text, images and videos.

My question is how can I get

page-(pk)pageId

articles-(pk)articleId,(fk)pageId.

SELECT pageTitle, GROUP_CONCAT(articleTitle) 
FROM pages 
JOIN articles ON articles.pageId = pages.pageId 
GROUP BY pageTitle

RESULTS: GOOD

joinPagesArticlesResult to join with:

articles(pk)articleId

text-(fk)articleId

images(fk)articleId

videos(fk)articleId

SELECT pageTitle, GROUP_CONCAT(articleTitle) AS articleTitle, 
group_concat(textId) AS textId, group_concat(text) AS text, 
group_concat(imageId) AS imageId,group_concat(imageLoc) AS imageLoc , 
group_concat(videoId) AS videoId,group_concat(videoLoc) AS videoLoc 
FROM articles 
JOIN pages ON pages.pageId = articles.pageId  
JOIN text ON text.articleId = articles.articleId
JOIN images ON images.articleId = articles.articleId
JOIN videos ON videos.articleId = articles.articleId
GROUP BY pageTitle

RESULTS: MISSING TYSON1 from GROUP_CONCAT(articleTitle)

joinTextImagesVideos

I want to be able to join all on one table. As well as get tyson1 to display in query. Not sure why it's not displaying. Im not sure if im doing the right type of join, if I need two SELECT statements to get this data, or if im not doing alias correctly.

The columns in each table go as follow

pages:

页面列

articles:

文章专栏

text:

文字栏

images:

图像列

Videos:

视频专栏

Try next query that use left join:

SELECT
    pageTitle,
    GROUP_CONCAT(articleTitle) AS articleTitles,
    GROUP_CONCAT(textId) AS textIds,
    GROUP_CONCAT(text) AS texts,
    GROUP_CONCAT(imageId) AS imageIds
    GROUP_CONCAT(imageLoc) AS imageLocs,
    GROUP_CONCAT(videoId) AS videoIds,
    GROUP_CONCAT(videoLoc) AS videoLocs
FROM
    articles
JOIN
    pages ON pages.pageId = articles.pageId
LEFT JOIN
    text ON text.articleId = articles.articleId
LEFT JOIN
    images ON images.articleId = articles.articleId
LEFT JOIN
    videos ON videos.articleId = articles.articleId
GROUP BY
    pageTitle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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