2017-06-15 28 views
0

我有以下查詢,我想tags.tag成分隔1列,產品表支點tagtable得到所有標籤在一列

SELECT products.* 
FROM products 
WHERE product.id IN ('13', '14') 

product_tag table  
id 
product_id 
tag_id 

tags table 
id 
tag 

期望的結果:

product.name , product.description , tags (colulumn with tags seperated by ,) ....

+1

請參閱https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat – Serg

回答

1
SELECT pr.name,pr.description,group_concat(tg.tags) 
FROM products pr 
INNER JOIN product_tag pt on pr.id=pt.product_id 
INNER JOIN tags tg on tg.id=pt.tag_id 
WHERE pr.id IN ('13', '14') 
group by pr.name,pr.description 

試試上面的代碼。

希望得到這個幫助。

+0

返回空,您忘記了product_tag列嗎?有3個表格,1個支點連接 – Bas

+0

嘗試更新一個。 –

+0

它有效,thnx! – Bas