SELECT
id AS class_id,
`name` AS class_name, /* 班级名称 */
(SELECT st.`name` FROM student st WHERE st.class_id=c.id) AS monitor_name, /* 班长名称 */
COUNT(*) AS class_std_count, /* 班级人数 */
(SELECT COUNT(*) FROM `group` WHERE `type`='算法组' AND class_id=c.id) AS sfz_count, /* 班级算法小组个数 */
(SELECT COUNT(*) FROM `group` WHERE `type`='大数据组' AND class_id=c.id) AS dsjz_count /* 班级大数据小组个数 */
FROM `class` c
LEFT JOIN student s ON s.class_id=c.id
GROUP BY c.id
使用 group by , 表关联。
select t.,t2.count from class t left join (select classid,count() count from student group by classid) t2 on t.classid=t2.classid