Mysql
 sql >> Database >  >> RDS >> Mysql

Selezione di un massimo di una somma di due colonne

Se desideri che tutte le righe siano presenti più righe con lo stesso valore massimo, puoi utilizzare questa query:

SELECT * FROM Table1
WHERE stu1Vers + stu2Vers = (SELECT MAX(stu1Vers + stu2Vers) FROM Table1)

Compreso la tua condizione:

SELECT * FROM Table1
WHERE stu1Vers + stu2Vers = (
    SELECT MAX(stu1Vers + stu2Vers)
    FROM Table1
    WHERE stu1!=stu2 and assignmentid=9
) AND stu1!=stu2 and assignmentid=9

Risultato:

392, 19, 2, 1, 6

Per quanto riguarda il tuo aggiornamento alla domanda, non sono sicuro di cosa intendi per restituire tutte le righe raggruppate per stu1 e stu2. Forse intendi ordinato per queste colonne? In tal caso, aggiungi ORDER BY stu1, stu2 alla domanda.