我有4张桌子
Class(ID,Name)
Month(ID,Name)
Student(ID,Name)
Examination(ID,Student,Class,Month,TotalMarks,ObtainedMarks)
列student,class,month
已作为Examination
表格中的参考。
我想从Examination
表order by ObtanedMarks and Group by Class
我使用了以下SQL查询
SELECT Months.Name, Class.Name AS Classname, Students.Name AS Studentname, Examination.*
FROM Class INNER JOIN
Examination ON Class.ID = Examination.Class INNER JOIN
Months ON Examination.Month = Months.ID INNER JOIN
Students ON Examination.Student = Students.ID
order by Obtained desc Group by Class.Name
但查询对我不起作用。
答案 0 :(得分:2)
看起来错误的方式,Group By
在Order By
之前。您需要在Group By
中添加所有列,或者在字段中使用聚合函数Sum
,Max
等不在Group By
条款中。
查看更新的查询...
SELECT Months.Name ,
Class.Name AS Classname ,
Students.Name AS Studentname ,
E.Student ,
E.Class ,
E.[Month] ,
E.TotalMarks ,
E.ObtainedMarks
FROM Class
INNER JOIN Examination E ON Class.ID = E.Class
INNER JOIN Months ON E.[Month] = Months.ID
INNER JOIN Students ON E.Student = Students.ID
GROUP BY Class.Name ,
Months.Name ,
Students.Name ,
E.Student ,
E.Class ,
E.[Month] ,
E.TotalMarks ,
E.ObtainedMarks
ORDER BY Obtained DESC