繁体   English   中英

mysql从一个表中选择,concat结果从另一个表中选择

[英]mysql select from one table with concat results from another table

因此,我需要从一个表中选择不同的值,但将同一表中的所有相关值都连接到另一个表中。

基本上,我遵循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

但是,如果特定的docs.id具有多个标记,则会重复这些行。 我想在每一行上得到的最终结果是:

| ID | orig_file | date_sent | alltags |

带有所需结果的示例:

| X | example_value.pdf | 2012-03-23 10:14:05 | tag_ex_1, tag_ex_2, etc |

集团联谊:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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