如何在mysql中限制使用group by

时间:2014-10-30 07:55:51

标签: mysql

我有一个很大的问题。我不知道如何限制使用group by。下面我有12套学生。我想限制10名学生。我想删除底部的最后2个。当我尝试使用限制10时,它只返回10行。我尝试了搜索,然后尝试了下面的代码:

SELECT 
   VSG.STUDENT_ID
   , VSG.SCHOOL_YEAR_ID
   , VSG.LEVEL_ID
   , VSG.FNAME
   , VSG.LNAME
   , VSG.MNAME
   , VSG.SUBJECT
   , VSG.GRADE
   ,
       (SELECT AVG(VSG.GRADE) FROM V_STUDENT_GRADES
        WHERE STUDENT_ID = VSG.STUDENT_ID
       ) AS AVG_GRADE
FROM V_STUDENT_GRADES VSG
JOIN 
(
  SELECT * FROM
  (
    SELECT STUDENT_ID, AVG(GRADE) AS AVG_GRADE FROM
    V_STUDENT_GRADES GROUP BY STUDENT_ID
  ) SG
  ORDER BY AVG_GRADE DESC LIMIT 10
) VG ON VG.STUDENT_ID=VSG.STUDENT_ID
WHERE SCHOOL_YEAR_ID = 2 
AND LEVEL_ID = 7 
AND PERIOD_ID =1 
AND SUBJECT_ID=SUBJECT_ID 
GROUP BY STUDENT_ID, SUBJECT 
ORDER BY AVG_GRADE DESC, SUBJECT ASC;

我有错误:

Error Code: 1242. Subquery returns more than 1 row

下面是12套学生的表格。我想要删除那些拥有 46 8 的STUDENT_ID的学生。

请帮忙。感谢。

+------------+----------------+----------+-----------+------------------------+-------------+--------------------+-------------+-------+-------------------+
| STUDENT_ID | SCHOOL_YEAR_ID | LEVEL_ID | PERIOD_ID | FNAME                  | LNAME       | MNAME              | SUBJECT     | GRADE | AVG_GRADE         |
+------------+----------------+----------+-----------+------------------------+-------------+--------------------+-------------+-------+-------------------+
|          4 |              2 |        7 |         1 | ASD                    | QWERTY      | ZXC                | math 100    |    97 | 90.66666666666667 |
|          4 |              2 |        7 |         1 | ASD                    | QWERTY      | ZXC                | CRIM 111    |    85 | 90.66666666666667 |
|          4 |              2 |        7 |         1 | ASD                    | QWERTY      | ZXC                | BIOLOGY     |    98 | 90.66666666666667 |
|          4 |              2 |        7 |         1 | ASD                    | QWERTY      | ZXC                | ELECTIVE 1  |    95 | 90.66666666666667 |
|          4 |              2 |        7 |         1 | ASD                    | QWERTY      | ZXC                | CIVIL 111   |    78 | 90.66666666666667 |
|          4 |              2 |        7 |         1 | ASD                    | QWERTY      | ZXC                | ENGLISH 222 |    96 | 90.66666666666667 |
|          4 |              2 |        7 |         1 | ASD                    | QWERTY      | ZXC                | COMPUTER    |    82 | 90.66666666666667 |
|          4 |              2 |        7 |         1 | ASD                    | QWERTY      | ZXC                | MAPEH       |    92 | 90.66666666666667 |
|          4 |              2 |        7 |         1 | ASD                    | QWERTY      | ZXC                | CONSTI 222  |    93 | 90.66666666666667 |
|         34 |              2 |        7 |         1 | Edeline                | Teh         | S                  | MAPEH       |    95 |                90 |
|         34 |              2 |        7 |         1 | Edeline                | Teh         | S                  | CONSTI 222  |    84 |                90 |
|         34 |              2 |        7 |         1 | Edeline                | Teh         | S                  | math 100    |    97 |                90 |
|         34 |              2 |        7 |         1 | Edeline                | Teh         | S                  | CRIM 111    |    87 |                90 |
|         34 |              2 |        7 |         1 | Edeline                | Teh         | S                  | BIOLOGY     |    90 |                90 |
|         34 |              2 |        7 |         1 | Edeline                | Teh         | S                  | ELECTIVE 1  |    93 |                90 |
|         34 |              2 |        7 |         1 | Edeline                | Teh         | S                  | CIVIL 111   |    84 |                90 |
|         34 |              2 |        7 |         1 | Edeline                | Teh         | S                  | ENGLISH 222 |    98 |                90 |
|         34 |              2 |        7 |         1 | Edeline                | Teh         | S                  | COMPUTER    |    82 |                90 |
|         31 |              2 |        7 |         1 | John Farid             | Macaludos   | s                  | math 100    |    74 | 89.55555555555556 |
|         31 |              2 |        7 |         1 | John Farid             | Macaludos   | s                  | CRIM 111    |    89 | 89.55555555555556 |
|         31 |              2 |        7 |         1 | John Farid             | Macaludos   | s                  | BIOLOGY     |    89 | 89.55555555555556 |
|         31 |              2 |        7 |         1 | John Farid             | Macaludos   | s                  | ELECTIVE 1  |    96 | 89.55555555555556 |
|         31 |              2 |        7 |         1 | John Farid             | Macaludos   | s                  | CIVIL 111   |    95 | 89.55555555555556 |
|         31 |              2 |        7 |         1 | John Farid             | Macaludos   | s                  | ENGLISH 222 |    98 | 89.55555555555556 |
|         31 |              2 |        7 |         1 | John Farid             | Macaludos   | s                  | COMPUTER    |    87 | 89.55555555555556 |
|         31 |              2 |        7 |         1 | John Farid             | Macaludos   | s                  | MAPEH       |    96 | 89.55555555555556 |
|         31 |              2 |        7 |         1 | John Farid             | Macaludos   | s                  | CONSTI 222  |    82 | 89.55555555555556 |
|         15 |              2 |        7 |         1 | Dealson                | Bagorio     | S                  | MAPEH       |    98 | 88.77777777777777 |
|         15 |              2 |        7 |         1 | Dealson                | Bagorio     | S                  | CONSTI 222  |    83 | 88.77777777777777 |
|         15 |              2 |        7 |         1 | Dealson                | Bagorio     | S                  | math 100    |    80 | 88.77777777777777 |
|         15 |              2 |        7 |         1 | Dealson                | Bagorio     | S                  | CRIM 111    |    99 | 88.77777777777777 |
|         15 |              2 |        7 |         1 | Dealson                | Bagorio     | S                  | BIOLOGY     |    98 | 88.77777777777777 |
|         15 |              2 |        7 |         1 | Dealson                | Bagorio     | S                  | ELECTIVE 1  |    96 | 88.77777777777777 |
|         15 |              2 |        7 |         1 | Dealson                | Bagorio     | S                  | CIVIL 111   |    96 | 88.77777777777777 |
|         15 |              2 |        7 |         1 | Dealson                | Bagorio     | S                  | ENGLISH 222 |    75 | 88.77777777777777 |
|         15 |              2 |        7 |         1 | Dealson                | Bagorio     | S                  | COMPUTER    |    74 | 88.77777777777777 |
|          6 |              2 |        7 |         1 | Gintoki                | Sakata      | G                  | CIVIL 111   |    71 | 88.55555555555556 |
|          6 |              2 |        7 |         1 | Gintoki                | Sakata      | G                  | ENGLISH 222 |    98 | 88.55555555555556 |
|          6 |              2 |        7 |         1 | Gintoki                | Sakata      | G                  | COMPUTER    |    74 | 88.55555555555556 |
|          6 |              2 |        7 |         1 | Gintoki                | Sakata      | G                  | MAPEH       |    92 | 88.55555555555556 |
|          6 |              2 |        7 |         1 | Gintoki                | Sakata      | G                  | CONSTI 222  |    95 | 88.55555555555556 |
|          6 |              2 |        7 |         1 | Gintoki                | Sakata      | G                  | math 100    |    99 | 88.55555555555556 |
|          6 |              2 |        7 |         1 | Gintoki                | Sakata      | G                  | CRIM 111    |    85 | 88.55555555555556 |
|          6 |              2 |        7 |         1 | Gintoki                | Sakata      | G                  | BIOLOGY     |    98 | 88.55555555555556 |
|          6 |              2 |        7 |         1 | Gintoki                | Sakata      | G                  | ELECTIVE 1  |    85 | 88.55555555555556 |
|          7 |              2 |        7 |         1 | OfficeDump             | SuperSchool | AccountsReceivable | ELECTIVE 1  |    84 | 88.33333333333333 |
|          7 |              2 |        7 |         1 | OfficeDump             | SuperSchool | AccountsReceivable | CIVIL 111   |    73 | 88.33333333333333 |
|          7 |              2 |        7 |         1 | OfficeDump             | SuperSchool | AccountsReceivable | ENGLISH 222 |    98 | 88.33333333333333 |
|          7 |              2 |        7 |         1 | OfficeDump             | SuperSchool | AccountsReceivable | COMPUTER    |    72 | 88.33333333333333 |
|          7 |              2 |        7 |         1 | OfficeDump             | SuperSchool | AccountsReceivable | MAPEH       |    93 | 88.33333333333333 |
|          7 |              2 |        7 |         1 | OfficeDump             | SuperSchool | AccountsReceivable | CONSTI 222  |    92 | 88.33333333333333 |
|          7 |              2 |        7 |         1 | OfficeDump             | SuperSchool | AccountsReceivable | math 100    |    98 | 88.33333333333333 |
|          7 |              2 |        7 |         1 | OfficeDump             | SuperSchool | AccountsReceivable | CRIM 111    |    98 | 88.33333333333333 |
|          7 |              2 |        7 |         1 | OfficeDump             | SuperSchool | AccountsReceivable | BIOLOGY     |    87 | 88.33333333333333 |
|         21 |              2 |        7 |         1 | Nivea                  | Gegone      | M                  | CRIM 111    |    87 | 88.22222222222223 |
|         21 |              2 |        7 |         1 | Nivea                  | Gegone      | M                  | BIOLOGY     |    89 | 88.22222222222223 |
|         21 |              2 |        7 |         1 | Nivea                  | Gegone      | M                  | ELECTIVE 1  |    98 | 88.22222222222223 |
|         21 |              2 |        7 |         1 | Nivea                  | Gegone      | M                  | CIVIL 111   |    95 | 88.22222222222223 |
|         21 |              2 |        7 |         1 | Nivea                  | Gegone      | M                  | ENGLISH 222 |    98 | 88.22222222222223 |
|         21 |              2 |        7 |         1 | Nivea                  | Gegone      | M                  | COMPUTER    |    72 | 88.22222222222223 |
|         21 |              2 |        7 |         1 | Nivea                  | Gegone      | M                  | MAPEH       |    98 | 88.22222222222223 |
|         21 |              2 |        7 |         1 | Nivea                  | Gegone      | M                  | CONSTI 222  |    82 | 88.22222222222223 |
|         21 |              2 |        7 |         1 | Nivea                  | Gegone      | M                  | math 100    |    75 | 88.22222222222223 |
|         13 |              2 |        7 |         1 | Jasima                 | Alonto      | D                  | ENGLISH 222 |    82 | 87.55555555555556 |
|         13 |              2 |        7 |         1 | Jasima                 | Alonto      | D                  | COMPUTER    |    83 | 87.55555555555556 |
|         13 |              2 |        7 |         1 | Jasima                 | Alonto      | D                  | MAPEH       |    98 | 87.55555555555556 |
|         13 |              2 |        7 |         1 | Jasima                 | Alonto      | D                  | CONSTI 222  |    94 | 87.55555555555556 |
|         13 |              2 |        7 |         1 | Jasima                 | Alonto      | D                  | math 100    |   100 | 87.55555555555556 |
|         13 |              2 |        7 |         1 | Jasima                 | Alonto      | D                  | CRIM 111    |    72 | 87.55555555555556 |
|         13 |              2 |        7 |         1 | Jasima                 | Alonto      | D                  | BIOLOGY     |    87 | 87.55555555555556 |
|         13 |              2 |        7 |         1 | Jasima                 | Alonto      | D                  | ELECTIVE 1  |    79 | 87.55555555555556 |
|         13 |              2 |        7 |         1 | Jasima                 | Alonto      | D                  | CIVIL 111   |    93 | 87.55555555555556 |
|         12 |              2 |        7 |         1 | Roderick               | Almero      | A                  | MAPEH       |    92 | 87.22222222222223 |
|         12 |              2 |        7 |         1 | Roderick               | Almero      | A                  | CONSTI 222  |    96 | 87.22222222222223 |
|         12 |              2 |        7 |         1 | Roderick               | Almero      | A                  | math 100    |   100 | 87.22222222222223 |
|         12 |              2 |        7 |         1 | Roderick               | Almero      | A                  | CRIM 111    |    79 | 87.22222222222223 |
|         12 |              2 |        7 |         1 | Roderick               | Almero      | A                  | BIOLOGY     |    87 | 87.22222222222223 |
|         12 |              2 |        7 |         1 | Roderick               | Almero      | A                  | ELECTIVE 1  |    74 | 87.22222222222223 |
|         12 |              2 |        7 |         1 | Roderick               | Almero      | A                  | CIVIL 111   |    75 | 87.22222222222223 |
|         12 |              2 |        7 |         1 | Roderick               | Almero      | A                  | ENGLISH 222 |   100 | 87.22222222222223 |
|         12 |              2 |        7 |         1 | Roderick               | Almero      | A                  | COMPUTER    |    82 | 87.22222222222223 |
|         10 |              2 |        7 |         1 | MA. LOURDES ANTOINNETE | THREE ROSES | DE ASIS            | BIOLOGY     |    78 | 86.88888888888889 |
|         10 |              2 |        7 |         1 | MA. LOURDES ANTOINNETE | THREE ROSES | DE ASIS            | ELECTIVE 1  |    92 | 86.88888888888889 |
|         10 |              2 |        7 |         1 | MA. LOURDES ANTOINNETE | THREE ROSES | DE ASIS            | CIVIL 111   |    94 | 86.88888888888889 |
|         10 |              2 |        7 |         1 | MA. LOURDES ANTOINNETE | THREE ROSES | DE ASIS            | ENGLISH 222 |    97 | 86.88888888888889 |
|         10 |              2 |        7 |         1 | MA. LOURDES ANTOINNETE | THREE ROSES | DE ASIS            | COMPUTER    |    75 | 86.88888888888889 |
|         10 |              2 |        7 |         1 | MA. LOURDES ANTOINNETE | THREE ROSES | DE ASIS            | MAPEH       |    96 | 86.88888888888889 |
|         10 |              2 |        7 |         1 | MA. LOURDES ANTOINNETE | THREE ROSES | DE ASIS            | CONSTI 222  |    83 | 86.88888888888889 |
|         10 |              2 |        7 |         1 | MA. LOURDES ANTOINNETE | THREE ROSES | DE ASIS            | math 100    |    84 | 86.88888888888889 |
|         10 |              2 |        7 |         1 | MA. LOURDES ANTOINNETE | THREE ROSES | DE ASIS            | CRIM 111    |    83 | 86.88888888888889 |
|         46 |              2 |        7 |         1 | Archi                  | Masa        | A                  | ENGLISH 222 |    81 | 86.22222222222223 |
|         46 |              2 |        7 |         1 | Archi                  | Masa        | A                  | COMPUTER    |    82 | 86.22222222222223 |
|         46 |              2 |        7 |         1 | Archi                  | Masa        | A                  | MAPEH       |    99 | 86.22222222222223 |
|         46 |              2 |        7 |         1 | Archi                  | Masa        | A                  | CONSTI 222  |    92 | 86.22222222222223 |
|         46 |              2 |        7 |         1 | Archi                  | Masa        | A                  | math 100    |   100 | 86.22222222222223 |
|         46 |              2 |        7 |         1 | Archi                  | Masa        | A                  | CRIM 111    |    75 | 86.22222222222223 |
|         46 |              2 |        7 |         1 | Archi                  | Masa        | A                  | BIOLOGY     |    86 | 86.22222222222223 |
|         46 |              2 |        7 |         1 | Archi                  | Masa        | A                  | ELECTIVE 1  |    76 | 86.22222222222223 |
|         46 |              2 |        7 |         1 | Archi                  | Masa        | A                  | CIVIL 111   |    85 | 86.22222222222223 |
|          8 |              2 |        7 |         1 | FN                     | LN          | MN                 | CONSTI 222  |    94 | 82.88888888888889 |
|          8 |              2 |        7 |         1 | FN                     | LN          | MN                 | math 100    |   100 | 82.88888888888889 |
|          8 |              2 |        7 |         1 | FN                     | LN          | MN                 | CRIM 111    |    78 | 82.88888888888889 |
|          8 |              2 |        7 |         1 | FN                     | LN          | MN                 | BIOLOGY     |    78 | 82.88888888888889 |
|          8 |              2 |        7 |         1 | FN                     | LN          | MN                 | ELECTIVE 1  |    82 | 82.88888888888889 |
|          8 |              2 |        7 |         1 | FN                     | LN          | MN                 | CIVIL 111   |    76 | 82.88888888888889 |
|          8 |              2 |        7 |         1 | FN                     | LN          | MN                 | ENGLISH 222 |    65 | 82.88888888888889 |
|          8 |              2 |        7 |         1 | FN                     | LN          | MN                 | COMPUTER    |    79 | 82.88888888888889 |
|          8 |              2 |        7 |         1 | FN                     | LN          | MN                 | MAPEH       |    94 | 82.88888888888889 |
+------------+----------------+----------+-----------+------------------------+-------------+--------------------+-------------+-------+-------------------+

1 个答案:

答案 0 :(得分:0)

试试这个。

...... GROUP BY STUDENT_ID(从TableName LIMIT 10中选择DISTINCT STUDENT_ID),AVG_GRADE DESC,SUBJECT ASC的主题顺序;

我认为这会对你有帮助。