1

我有一个STUDENT表,其中包含有关教师学生的数据:

STUDENT(ID, Name, Grade, ID_Tutor)

结构数据

CREATE TABLE STUDENT
    (`ID` int, `Name` varchar(5), `Grade` int, `ID_Tutor` int);

INSERT INTO STUDENT
    (`ID`, `Name`, `Grade`, `ID_Tutor`)
VALUES
    (1,    'A',     10,       NULL),
    (2,    'B',     9.5,      1),
    (3,    'C',     9,        1),
    (4,    'D',     8,        1),
    (5,    'E',     7,        4),
    (6,    'F',     8,        1),
    (7,    'G',     5,        7),
    (10,   'H',     6,        5)
    (11,   'I',     7,        3),
    (12,   'J',     9,        10),
    (13,   'K',     9.6,      11),
    (14,   'L',     8.9,      5),
    (15,   'M',     9.9,      13),
    (16,   'N',     7,        2),
    (17,   'O',     7.9,      2),
    (18,   'P',     2,        15);

前5名教师成绩:

    `ID`, `Name`, `Grade`, `ID_Tutor`
    (1,    'A',     10,       NULL),
    (15,   'M',     9.9,      13),
    (13,   'K',     9.6,      11),
    (2,    'B',     9.5,      1),
    (3,    'C',     9,        1),
    (12,   'J',     9,        10),

结果应该出现上表中这些导师的学生。

我想做一个 SQL 查询 ( MYSQL ) 来查找

  • 学生姓名
  • 导师姓名
  • 导师等级

对于导师的成绩在前5名教师学生中的学生(并且不使用LIMIT和/或ROWNUM- 对于 Oracle 解决方案)。

结果应如下所示:

| student name | tutor name | tutor grade |
       'B'           'A'          10           
       'C'           'A'          10           
       'D'           'A'          10           
       'F'           'A'          10           
       'P'           'M'           9.9           
       'M'           'K'           9.6              
       'N'           'B'           9.5            
       'O'           'B'           9.5            
       'I'           'C'           9            

我尝试了这样的事情(但没有工作),我认为我很复杂......(我不应该使用ROWNUM

SELECT 
    s.ID, 
    s.Name Student, 
    s.Grade, 
    p.ID_Tutor, 
    p.Grade
FROM 
    STUDENT s,
    (SELECT * FROM 
        (SELECT * FROM 
            (SELECT 
                t.Name Tutor , 
                t.ID , 
                Grade Grade_Tutor 
            FROM STUDENT s
                JOIN STUDENT t 
                    on s.ID_Tutor = t.ID
            GROUP BY t.Name, t.ID)
        ORDER BY 3 desc ) 
    WHERE ROWNUM < 6) p
WHERE s.ID_Tutor = p.ID

给我这个错误

    #1248 - Every derived table must have its own alias

提前致谢!

4

2 回答 2

1

您的代码缺少几个表别名。检查这个有效的SQL Fiddle 代码。但它没有返回任何值,因为我只是添加了一些愚蠢的值。随意使用它并测试您的结果。

SELECT s.ID, s.Name Student, s.Grade, p.ID_Tutor, p.Grade_tutor
FROM STUDENT s,
    (SELECT * FROM 
        (SELECT * FROM 
            (SELECT t.Name Tutor, t.ID , s.Grade Grade_Tutor, s.ID_Tutor
               FROM STUDENT s
               JOIN STUDENT t
                    on s.ID_Tutor = t.ID
            GROUP BY t.Name, t.ID) innerTable
         ORDER BY 3 desc ) secondInnerTable
     WHERE @ROWNUM < 6) p
WHERE s.ID_Tutor = p.ID
于 2012-09-10T08:48:51.597 回答
1

非常感谢@TehShrike,这是他的回答!!!

唯一的“缺点”是使用 LIMIT,但如果有人有没有 LIMIT 的版本,请发布;在那之前,再次感谢@TehShrike

SELECT 
        student.NAME AS student_name, 
        top_five_tutors.NAME AS tutor_name, 
        top_five_tutors.grade AS tutor_grade
FROM (
        SELECT 
                tutor.id, 
                tutor.NAME, 
                tutor.Grade
        FROM (
                SELECT DISTINCT 
                                id_tutor AS id
                FROM student
             ) AS tutor_ids

        JOIN student AS tutor 
            ON tutor.id = tutor_ids.id

        ORDER BY tutor.grade DESC
            LIMIT 5
     ) AS top_five_tutors

JOIN student 
    ON student.id_tutor = top_five_tutors.id
于 2012-09-11T20:19:06.140 回答