Get the highest value from a Mysql column

1

I have a column where I store the values:

Nome | Turma | Nota1 | Nota2 | Nota3 | Nota4

How would I get the highest grade from the Note1, Note2, Note3, and Note4 fields? I understand that MAX () does this, but how could I apply to my need?

    
asked by anonymous 18.12.2018 / 01:06

2 answers

3

You can use CASE WHEN to solve your problem by performing the query as follows:

SELECT Nome, Turma,
CASE
       WHEN Nota1 >= Nota2 AND Nota1 >= Nota3 THEN Nota1
       WHEN Nota2 >= Nota1 AND Nota2 >= Nota3 THEN Nota2
       WHEN Nota3 >= Nota1 AND Nota3 >= Nota2 THEN Nota3
       ELSE                                        Nota4
END AS MaiorNota
FROM *suaTabela*

You can also use the GREATEST command by passing the number of parameters to be returned the highest value, for example:

SELECT Nome, Turma, GREATEST(Nota1, Nota2, Nota3, Nota4) AS MaiorNota FROM *suaTabela*

I hope I have helped!

    
18.12.2018 / 01:24
-1
SELECT GREATEST(Nota1, Nota2, Nota3, Nota4) FROM tabela WHERE id=1
    
18.12.2018 / 01:14