所以我需要从一个表中选择不同的值,但是在同一个查询中加入另一个表中的所有相关值.
基本上我遵循Toxi TagSystem架构http://forge.mysql.com/wiki/TagSchema#Toxi
三表之间的多对多映射.
我需要在每一行显示所有插入的值(docs),但是我希望其中一列包含文件用逗号分隔的所有标记.
现在我有
SELECT
docs.id AS id,
docs.orig_file AS orig_file,
docs.date_sent AS date_sent,
tags.tag_name AS alltags
FROM documat AS docs
LEFT JOIN documat_file2tag AS f2t ON f2t.doc_id = docs.id
LEFT JOIN documat_tags AS tags ON tags.id = f2t.tag_id
Run Code Online (Sandbox Code Playgroud)
但是,如果特定的docs.id具有多个标记,则会重复这些行.我希望每行的最终结果是:
| ID | orig_file | date_sent | alltags |
Run Code Online (Sandbox Code Playgroud)
使用所需的结果示例:
| X | example_value.pdf | 2012-03-23 10:14:05 | tag_ex_1, tag_ex_2, etc |
Run Code Online (Sandbox Code Playgroud)
集团Concat:
SELECT
docs.id AS id,
docs.orig_file AS orig_file,
docs.date_sent AS date_sent,
GROUP_CONCAT(distinct tags.tag_name) AS alltags
FROM documat AS docs
LEFT JOIN documat_file2tag AS f2t ON f2t.doc_id = docs.id
LEFT JOIN documat_tags AS tags ON tags.id = f2t.tag_id
GROUP BY docs.id
Run Code Online (Sandbox Code Playgroud)