2010-10-27 88 views
1

我有2代表這樣mysql的連接查詢

Table 1 

id name 
1 ABC 
2 DEF 
3 GEF 


Table 2 
name meal 
ABC m1 
ABC m2 
GEF m1 

Table 3 
meal detail 
m1 mutton 
m2 beaf 

我怎樣才能得到輸出這樣?

Id name meal_detail 
1 ABC mutton,beaf 
2 DEF 
3 GEF mutton 

由於提前

回答

3
SELECT t1.id, 
      t1.`name`, 
      GROUP_CONCAT(t3.detail) AS `meal_detail` 
    FROM t1 
LEFT JOIN t2 ON t2.`name` = t1.`name` 
LEFT JOIN t3 ON t3.meal = t2.meal 
GROUP BY t1.`name` 
+0

+1:以爲我將不得不格式化你:P – 2010-10-27 00:15:52

+0

@OMG小馬:不,現在我按照你的格式類型,到處;-)只是作弊與保存正確的但沒有格式化的查詢趕上第一個答案;-P – zerkms 2010-10-27 00:17:31

+0

@EarnWhileLearn:幸運我;-) – zerkms 2010-10-27 00:41:23