2014-01-19 58 views
0

我有兩個表 - 一個用於課程,另一個用於參加課程的人。mysql左加入,按計數分組

兩者都是由COURSE_ID

表1加入:firstaid - 擁有所有課程名稱

表2:first_aid_att - 記錄與會者和捕捉急救 課程ID

我希望獲得每門課程的參加者人數。

我有一個加入工作,但不知道如何做組和計數。

SELECT * 
FROM firstaid 
LEFT JOIN first_aid_att ON firstaid.course_id = first_aid_att.course_id 
ORDER BY `sortDate` ASC 

回答

1

試試這個:

SELECT f1.*, COUNT(f2.course_id) 
FROM firstaid f1 
LEFT JOIN first_aid_att f2 ON (f1.course_id = f2.course_id) 
GROUP BY f1.course_id 
ORDER BY f2.sortDate 
+0

質量 - 感謝賈斯汀 – Jeff