2015-01-08 60 views
0

輸出:https://www.dropbox.com/s/q9bjrzndbzj0l2i/test3.PNG?dl=0閱讀ROW_NUMBER

如果我想只得到位置(ROW_NUMBER)= 3,如果讓我們說的Stud_ID = 4如何合併查詢到當前的代碼?

SET @row_num = 0; 
SELECT @row_num := @row_num + 1 as Position, s.* 
FROM 
(
    SELECT 
     Student.Stud_ID, Student.Stud_Name, Student.Stud_Class, SUM(Grade.Percentage) AS Points 
    FROM Student, Student_Subject, Grade 
    WHERE Student.Stud_ID = Student_Subject.Stud_ID 
     AND Student_Subject.Stud_Subj_ID = Grade.Stud_Subj_ID 
     AND Student.Stud_Form = '1' 
     AND Grade.Quarter = '1' 
    GROUP BY Student.Stud_ID 
    ORDER BY Points DESC 
) AS s; 

謝謝!!

回答

1

使用子查詢過您的查詢與和所需行數過濾它

SELECT * 
FROM (
SELECT @row_num := @row_num + 1 AS `Position`, s.* 
FROM 
(
    SELECT 
     Student.Stud_ID, Student.Stud_Name, Student.Stud_Class, SUM(Grade.Percentage) AS Points 
    FROM Student, Student_Subject, Grade 
    WHERE Student.Stud_ID = Student_Subject.Stud_ID 
     AND Student_Subject.Stud_Subj_ID = Grade.Stud_Subj_ID 
     AND Student.Stud_Form = '1' 
     AND Grade.Quarter = '1' 
    GROUP BY Student.Stud_ID 
    ORDER BY Points DESC 
) AS s 
CROSS JOIN (SELECT @row_num := 0) AS s1 
) AS t 
WHERE t.Position = 3 
+1

明白了!謝啦 –