如何在MYSQL中连接查找表中的字符串值

时间:2012-06-28 10:49:16

标签: mysql

我有三个表:upload,tag和upload_tag。上传保存文件上传的详细信息,标记保存标记名称,upload_tag是一个查找表,它将上传ID与标记ID匹配,即:

+--------------------------------------------------------+
| upload                                                 |
+--------------------------------------------------------+
|id         |name                                        |
+-----------+--------------------------------------------+
|1          |file_1.jpg                                  |
+-----------+--------------------------------------------+
|2          |file_2.doc                                  |
+-----------+--------------------------------------------+

+--------------------------------------------------------+
|tag                                                     |
+--------------------------------------------------------+
|id         |name                                        |
+-----------+--------------------------------------------+
|1          |a_tag                                       |
+-----------+--------------------------------------------+
|2          |b_tag                                       |
+-----------+--------------------------------------------+
|3          |c_tag                                       |
+-----------+--------------------------------------------+

+--------------------------------------------------------+
|upload_tag                                              |
+-----------+--------------------------------------------+
|upload_id  |tag_id                                      |
+-----------+--------------------------------------------+
|1          |1                                           |
+-----------+--------------------------------------------+
|1          |2                                           |
+-----------+--------------------------------------------+
|2          |3                                           |
+-----------+--------------------------------------------+
|2          |2                                           |
+-----------+--------------------------------------------+

我需要使用标记字符串从数据库中提取上传内容,但所有内容都在一行中,因此结果如下所示:

+----------------------------------------------------+
|filename      |tags                                 |
+--------------+-------------------------------------+
|file_1.jpg    |a_tag; b_tag                         |
+--------------+-------------------------------------+
|file_2.doc    |c_tag; b_tag                         |
+--------------+-------------------------------------+

目前我认为最好的方法是创建一个MySQL函数,它接受一个上传ID并返回格式化的标签字符串。我在这里是否正确,或者有更好的方法来实现这一目标吗?

2 个答案:

答案 0 :(得分:2)

您应该使用LEFT JOIN子句和GROUP_CONCAT()函数。

SELECT          u.name AS filename,
                GROUP_CONCAT(t.name SEPARATOR '; ') AS tags
    FROM        upload u
    LEFT JOIN   upload_tags ut
        ON      u.id = ut.upload_id
    LEFT JOIN   tag t
        ON      t.id = ut.tag_id
    GROUP BY    u.id;

GROUP_CONCAT是一个聚合函数,意味着它适用于多行(组织为组),而不是CONCAT,它适用于多个列。

答案 1 :(得分:1)

试试这个::

select 
upload.name as filename
GROUP_CONCAT(tag.name SEPARATOR '; ') as tags
from 
upload_tag ut 
inner join upload  on (ut.upload_id = upload.id)
inner join tags on (ut.tag_id = tags.id) 
group by ut.upload_id