0

我有三个表:上传、标签和上传标签。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 并返回格式化的标签字符串。我在这里是正确的,还是有更好的方法来实现这一点?

4

2 回答 2

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适用于多列。

于 2012-06-28T10:51:17.517 回答
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
于 2012-06-28T10:57:35.823 回答