2015-01-08 3 views
0

ROW_NUMBER : 나는 Stud_ID = 4를 말할 수있는 경우 = 3 단지 위치 (ROW_NUMBER)를 얻으려면 https://www.dropbox.com/s/q9bjrzndbzj0l2i/test3.PNG?dl=0읽기 출력

가 어떻게 현재의 코드로 쿼리를 포함합니까?

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

이있어 원하는 행 번호로 필터링! 고마워. –