MySQL结果排名

时间:2014-11-28 19:25:57

标签: mysql

我有一个名为enroll的mysql表,其中包含以下列:student,subject,mark,year:

student subject mark    year
John    Mathematics 70  2013
Peter   Mathematics 87  2013
James   Mathematics 69  2013
Paul    Mathematics 50  2013
Judas   Mathematics 77  2013
Jude    Mathematics 99  2013
Michael Mathematics 48  2013
Matthew Mathematics 50  2013

我想得到这些商标的等级,让我们说一下John WHERE subject ='数学' AND year =' 2013'

如何编写此SQL语句?

2 个答案:

答案 0 :(得分:0)

我自己对MySQL不太好,但如果我正确地按照你的问题应该如下:

SELECT * FROM 'enrol' WHERE student='John' AND subject='Mathematics' AND year='2013' ORDER BY 'mark' DESC

这将选择一个名为John的人做Mathmaatics和2013年的结果。

答案 1 :(得分:0)

如果我理解正确......

SELECT student
     , rank 
  FROM 
     ( SELECT x.*
            , IF(@prev<>mark,@i:=@i+1,@i:=@i) rank
            , @prev:=mark 
         FROM my_table x
            , (SELECT @i:=0,@prev:='') vars 
        WHERE year = 2013 
          AND subject = 'mathematics' 
        ORDER 
           BY mark DESC
     ) n 
 WHERE student = 'John';