這裏是我的數據庫結構:使用GROUP_CONCAT檢索來自外部表的標題列表
而這裏的SQL語句,我試圖:
SELECT
t1.Name AS Teacher_Name,
t2.Name AS Observer_Name,
o.Datetime AS Datetime,
o.Type AS Type,
o.Year_Group AS Year_Group,
o.Class_Name AS Class_Name,
c.Title AS Course_Name,
GROUP_CONCAT(f.Focus_ID) AS Focus,
o.Achievement_Grade AS Achievement_Grade,
o.Behaviour_Grade AS Behaviour_Grade,
o.Teaching_Grade AS Teaching_Grade,
o.Notes AS Notes
FROM observations o
INNER JOIN teachers t1 ON o.Teacher_ID = t1.Teacher_ID
INNER JOIN teachers t2 ON o.Observer_ID = t2.Teacher_ID
INNER JOIN courses c ON o.Course_ID = c.Course_ID
INNER JOIN foci f ON f.Observation_ID = o.ID
ORDER BY `Datetime` DESC LIMIT 0,10
我是什麼試圖實現是在上述聲明中檢索Foci
的列表與,
分隔符,如Appraisal,Post 16,Teaching and Learning
。
這將通過在Foci.Observation_ID是否滿足Observations.ID然後檢索每個重點從Focus_Labels.Title的名字,如果Foci.Focus_ID匹配Focus_Labels.ID焦點表中檢索每一行實現。
在上面的聲明中,我沒有得到儘可能檢索Focus_Labels.Title
,因爲我甚至無法獲得Foci.ID
的工作。
任何人都可以幫忙嗎?
我想你需要一個'GROUP BY'使用'GROUP_CONCAT' – HerrSerker
@dunc如果你用表和一些示例數據創建了一個[SQL Fiddle](http://sqlfiddle.com/)會很有幫助。 – Taryn