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