我想获得每个学生的平均资格。
例如,有30名学生有:
Pupil 1: Qualification 1: 5,6 Qualification 2: 3,2 Qualification 3: 9,1
Pupil 2: Qualification 1: 5,1 Qualification 2: 8,6 Qualification 3: 3,1
Pupil 3: Qualification 1: 1,9 Qualification 2: 7,2 Qualification 3: 5,1
Pupil 4: Qualification 1: 4,6 Qualification 2: 5,2 Qualification 3: 9,5
等...
我想得到:
我试过了:
select distinct pupils.name, pupils.surname, qualifications.id_trimester, round(avg(qualifications.qualification),2), count(qualifications.qualification) from pupils, qualifications where pupils.level='1' and pupils.class='A' and qualifications.id_trimester=1 and qualifications.type_qualification='class' group by pupils.surname.
但它显示了所有行的平均资格和计数。它显示了所有行中重复的第一个学生的平均资格......
表格是:
pupils:
id_pupil
name
surname
email
user
pass
level
class
qualifications:
id_qualification
qualification
date
time
subject
id_pupil
id_user
id_trimester
type_qualification (misses, delayes or attitude)
答案 0 :(得分:5)
您需要指定如何将qualifications
表连接到pupils
表。
select ... from pupils join qualifications using (id_pupil) where ...
答案 1 :(得分:1)
我看到三个问题。
GROUP BY id_pupil
而不是surname
。DISTINCT
- 事实上,在同一个SQL语句中使用DISTINCT
和GROUP BY
通常是个坏主意。