2011-07-21 14 views
1

私はアイテムテーブルとタグテーブルを持っています。今私が望むのは、特定の条件の下でアイテムを取得するSelectを持つことです。各アイテムに対して、関連するタグを選択し、それらを文字列に入れて別のフィールドにします。関連する要素の属性を文字列として選択する

例:

table_items: 
id | title 
--------------------------- 
01 | peter 
--------------------------- 
02 | john 
--------------------------- 
03 | cindy 

tags: 
id | title 
--------------------------- 
01 | tall 
--------------------------- 
02 | tiny 
--------------------------- 
03 | blone 
--------------------------- 
04 | loud 
--------------------------- 
05 | ... 

tags_to_items: 
itemid | tagid 
--------------------------- 
01  | 02 
--------------------------- 
01  | 04 
--------------------------- 
02  | 01 
... 

私はあなたのポイントを得ると思います。

Now I want a result like this: 
itemid | title | tags 
--------------------------- 
01  | peter | tiny, loud 
--------------------------- 
01  | john | tall, fast, bored 

MySQLでこれを行うことはできますか? どのようにですか?

+1

GROUP_CONCAT機能の表情、http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat –

答えて

2

MySQLでは、これを行うにはGROUP_CONCAT関数が必要です。

のような何か:

select ti.id, ti.title, group_concat(t.title) 
    from table_items ti 
inner join tag_to_items tti on (ti.id = tti.itemid) 
inner join tags t on (t.id = tti.tagid) 
group by ti.id, ti.title 
+0

ああ、ちょうど自分自身を発見したが、多くのありがとう! –

関連する問題