如何顯示班級學生人數(shù)的頻率。我在數(shù)據(jù)庫中有幾個表。表=類+----+---------+---------+| 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條經(jīng)驗 獲得超5個贊
要計算每個班級的學生人數(shù),您只需要:
SELECT id_kelas, COUNT(*) 'count'
FROM kelas_siswa
GROUP BY id_kelas
然后你可以添加額外JOIN的 s 來包含你需要的任何其他內(nèi)容,比如類元數(shù)據(jù)。
例如:
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
這還將列出所有學生的姓名以及計數(shù)。
- 1 回答
- 0 關(guān)注
- 102 瀏覽
添加回答
舉報
0/150
提交
取消