在MySql中,我有一个文档表,一个文件夹表和一个searchtags表
Documents
ID
title
Folders
ID
title
Searchtags
ID
title
docFolders
docID -> Documents.ID
folderID -> Folders.ID
docSearchtags
docID -> Documents.ID
searchtagID -> Searchtags.ID
在上面,docFolders和docSearchtags是多对多联接表,用于表达文档和文件夹以及文档和searchtags之间的关系。单个文档可以具有多个文件夹以及与其关联的多个搜索标签。
我想知道是否可以创建一个查询,该查询将选择所有文档的标题,以及属于每个文档的每个文件夹标题的简化列表,以及属于该文档的每个searchtag标题的简化列表每个文件。
例如,结果集可能看起来像这样:
RESULT
docTitle | folderTitles | searchtagTitles
The Quick Brown Fox | foxes, colours | Bushy tails, browny, foxy
The Slow Green Turtle | turtles, colours | Hard shells, slimy, turtle-soup
The Cute Fluffy Bunny | bunnies, cute | Fluffy, rabbit, rabbit-stew
在此先感谢您的帮助
最佳答案
采用:
SELECT d.title AS doctitle,
GROUP_CONCAT(DISTINCT f.title) AS foldertitles,
GROUP_CONCAT(st.title) AS searchtagtitles
FROM DOCUMENTS d
JOIN DOCFOLDER df ON df.docid = d.id
JOIN FOLDERS f ON f.id = dc.folderid
JOIN DOCSEARCHTAGS dsc ON dsc.docid = d.id
JOIN SEARCHTAGS st ON st.id = dsc.searchtagid
GROUP BY d.title