如何显示班级学生人数的频率。我在数据库中有几个表。表=类+----+---------+---------+| id | tingkat | nama |+----+---------+---------+| 1 | 5 | Kelas 5 || 2 | 3 | Kelas 3 |+----+---------+---------+表 = 学生+----+----------+| id | nama |+----+----------+| 1 | 1 || 2 | 1 || 3 | 1 || 4 | 1 |etc.表 = student_class+----+----------+----------+------+| id | id_kelas | id_siswa | ta |+----+----------+----------+------+| 18 | 1 | 18 | 2019 || 19 | 1 | 19 | 2019 || 20 | 1 | 20 | 2019 || 21 | 1 | 21 | 2019 || 22 | 1 | 22 | 2019 || 23 | 1 | 23 | 2019 || 24 | 1 | 24 | 2019 || 25 | 1 | 25 | 2019 || 26 | 1 | 26 | 2019 || 27 | 1 | 27 | 2019 || 28 | 1 | 28 | 2019 || 29 | 1 | 29 | 2019 || 30 | 1 | 30 | 2019 || 31 | 1 | 31 | 2019 || 1 | 2 | 1 | 2019 || 2 | 2 | 2 | 2019 || 3 | 2 | 3 | 2019 || 4 | 2 | 4 | 2019 || 5 | 2 | 5 | 2019 || 6 | 2 | 6 | 2019 || 7 | 2 | 7 | 2019 || 8 | 2 | 8 | 2019 || 9 | 2 | 9 | 2019 || 10 | 2 | 10 | 2019 || 11 | 2 | 11 | 2019 || 12 | 2 | 12 | 2019 || 13 | 2 | 13 | 2019 || 14 | 2 | 14 | 2019 || 15 | 2 | 15 | 2019 || 16 | 2 | 16 | 2019 || 17 | 2 | 17 | 2019 |+----+----------+----------+------+
1 回答
四季花海
TA贡献1811条经验 获得超5个赞
要计算每个班级的学生人数,您只需要:
SELECT id_kelas, COUNT(*) 'count'
FROM kelas_siswa
GROUP BY id_kelas
然后你可以添加额外JOIN的 s 来包含你需要的任何其他内容,比如类元数据。
例如:
SELECT ks.id_kelas, k.name, COUNT(*) 'count'
FROM kelas_siswa ks INNER JOIN kelas k
ON ks.id_kelas = k.id
GROUP BY ks.id_kelas
或者变得花哨:
SELECT ks.id_kelas, k.name, COUNT(*) 'count', GROUP_CONCAT(s.nama) 'students'
FROM kelas_siswa ks INNER JOIN kelas k
ON ks.id_kelas = k.id
INNER JOIN siswa s
ON ks.id_siswa = s.id
GROUP BY ks.id_kelas
这还将列出所有学生的姓名以及计数。
- 1 回答
- 0 关注
- 77 浏览
添加回答
举报
0/150
提交
取消