对于我的项目,我有一张学生桌和一张学校桌子。
学生属于公司。一所学校有很多学生。
(当该学生毕业时,学生的school_id被指定为NULL。)
The student table:
+----+------------+-----------+------------+
| id | first_name | last_name | school_id |
+----+------------+-----------+------------+
The school table:
+----+------------+------------------------+
| id | city_id | name |
+----+------------+------------------------+
目标是内部加入这些表格,计算每所学校的学生数量,并按学生编号排序。
答案 0 :(得分:0)
请考虑以下SQL查询:
SELECT school.NAME,
count(student.id) AS Total_Student
FROM school
LEFT JOIN student
ON school.id = student.school_id
GROUP BY school.id
ORDER BY Total_Student