2014-10-30 62 views
2

我有一個非常大的問題。我不知道如何限制使用group。下面我有12套學生。我想限制10組學生。我想刪除底部的最後2個。當我嘗試使用限制10時,它只返回10行。我試圖尋找,我嘗試下面的代碼:如何限制在mysql中使用group

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套學生的表。我想刪除具有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 | 
+------------+----------------+----------+-----------+------------------------+-------------+--------------------+-------------+-------+-------------------+ 

+0

數據庫規範化可能會顯着減少此數據庫上所需的工作量。將它帶入[第二範式](https://en.wikipedia.org/wiki/Second_normal_form),它應該防止更新和刪除異常。 – Padarom 2014-10-30 08:01:28

回答

0

試試這個。

...... GROUP BY STUDENT_ID IN(SELECT DISTINCT STUDENT_ID FROM TABLENAME LIMIT 10),SUBJECT ORDER BY AVG_GRADE DESC,SUBJECT ASC;

我想這會幫助你。

+0

這個版本是5.6嗎?我的版本是5.5。我如何在5.5中實現這個目標? – 2014-10-31 05:51:50