0

我有 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 

但查询对我不起作用。

4

1 回答 1

4

看起来它走错了路,Group By在前面Order By。您要么需要在 中包含要显示的所有列,要么在子句中没有的字段上Group By使用聚合函数Sum等。MaxGroup 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 
于 2013-09-16T12:55:33.590 回答