2

我有两张桌子;一个包含学生names和另一个是包含和他们sids的“接受”表。sids of studentsgrades

我想显示平均大于学生名字“Peter-Parker”的学生的名字。

我已经尝试了下面的查询,但它不起作用。

SELECT s.sid, s.fname, s.lname
FROM student s, take t
WHERE s.sid = t.sid AND AVG(t.grade) > ALL(
SELECT AVG(grade)
FROM take, student
WHERE student.fname = 'Ali' and student.lname='Demir');
 WITH AliAv(avg) AS
 (SELECT AVG(grade) from take t, student s
 where t.sid = s.sid ands.fname = 'Ali' and s.lname = 'Demir')
 select student.sid, student.fname, student.lname
 from student, take
 where student.sid = take.sid Group by student.sid
 having avg(take.grade) > AliAv.av;
4

2 回答 2

2
SELECT s.sid, s.fname, s.lname, AVG(t.grade) AS average
FROM student AS s
JOIN take AS t ON t.sid = s.sid
GROUP BY s.sid
HAVING average > (
    SELECT AVG(t2.grade)
    FROM student AS s2
    JOIN take AS t2 ON t2.sid = s2.sid
    WHERE s2.fname = 'Peter' and s2.lname = 'Parker'
)
于 2012-12-29T13:30:37.217 回答
0

这适用于 SQL Server,我不知道该语法在 MySQL 中是否有效:

SELECT *
FROM Student s
WHERE
  (SELECT AVG(Grade) FROM Take WHERE SID=s.ID) > (SELECT AVG(Grade) FROM Take WHERE SID = (SELECT SID FROM Student WHERE FName='Peter' AND LName='Parker'))
于 2012-12-29T13:42:02.110 回答