我有两张 table - 一张用于类(class),一张用于参加类(class)的人员。
两者都通过 course_id 连接
table 1: firstaid - has all the course names
table 2: first_aid_att - records attendees and captures first aid course id
我想了解每门类(class)的参加人数。
我已经有一个连接正在工作,但不知道如何进行分组和计数。
SELECT *
FROM firstaid
LEFT JOIN first_aid_att ON firstaid.course_id = first_aid_att.course_id
ORDER BY `sortDate` ASC
最佳答案
试试这个:
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
关于mysql 左连接,按计数分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21222544/