次のテーブルがあるとしましょう:
食べ物
ID | title
---+----------
1 | sandwich
2 | spaghetti
成分
ID | food_reference | type | location | bought
----+----------------+------+----------+----------
100 | 1 | ham | storeA | 11-1-2013
101 | 1 | jam | storeB | 11-1-2013
102 | 2 | tuna | storeB | 11-6-2013
そして、一致するすべての行、望ましい出力を見つける選択クエリが必要です:
{id:1,title:sandwich,ingridients:[
{id:100,type:ham,location:storeA,bought:11-1-2013},
{id:101,type:jam,location:storeB,bought:11-1-2013}],
id:2,title:spaghetti,ingridients:[
{id:102,type:tuna,location:storeB,bought:11-6-2013}]}
これまでのところ、次のようなものがあります。
SELECT F.id, F.title, group_concat(I.d_id ,I.type,I.location,I.bought SEPARATOR ',') as ingridients,
FROM food F, ingridients I
WHERE F.id=I.food_reference
GROUP BY F.id
;
問題は、これらすべての値が (明らかに) 連結されて、次の出力が得られることです。
{id:1,title:sandwich,ingridients:100hamstoreA11-1-2013,101jamstoreB11-1-2013},
{id:2,title:spaghetti,ingridients:102tunastoreB11-6-2013}
注: この出力は、オブジェクトで関数 json_encode を使用して作成されます
それで、それを行う方法について何か提案はありますか?