2014-01-28 135 views
0

我有3個表格(contents,tags,contents_tags)。Mysql,group by和子查詢在一行

它可在http://sqlfiddle.com/#!2/8b0338

我需要一個查詢返回這樣的事:

+------------+--------+----------------+ 
| contents.id| title | tag   | 
+------------+--------+----------------+ 
|  1  | News 1 | News, Articles | 
|  2  | News 2 | News   | 
+------------+--------+----------------+ 
+1

'JOIN' +'GROUP_CONCAT()' –

回答

2

您可以使用加入和GROUP_CONCAT

試試這個

select id, title, tag from contents 
join 
(select id_content, GROUP_CONCAT(tag) as tag from contents_tags 
    join tags on contents_tags.id_tag=tags.id group by id_content) t on 
contents.id=t.id_content 
+0

超凡!有用。謝謝。 –