我有三個表格:upload,tag和upload_tag。上傳保存文件上傳的細節,標籤持有標記名稱和upload_tag是一個查找表,它匹配的上傳ID,以標籤ID即:如何連接來自MYSQL中查找表的字符串值
+--------------------------------------------------------+
| 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,並返回標籤的格式化字符串。我在這裏糾正,還是有更好的方法來實現這一目標?
可能重複[如何從MySQL SELECT串聯值(http://stackoverflow.com/questions/1091746/how-to-concatenate-values-from-mysql - 選擇) – Ken
@Ken:這有點類似但是不重複... –