Randomized query in ascending order in MYSQL

4

I have a table where I have the student's name and punctuation, I need to pick up 5 random students and display them in ascending order of punctuation.

I tried to do this, but he's just bringing students randomly and not ordering the score:

SELECT * FROM 'aluno' ORDER BY RAND() , 'AlunoPontos' ASC LIMIT 0 , 5

Does anyone know how I can do this type of query?

Thank you.

    
asked by anonymous 27.01.2016 / 15:30

1 answer

5

One way to achieve the result you expect is to use sub-query.

SELECT * FROM (
      SELECT * FROM 'aluno' ORDER BY RAND() LIMIT 0, 5
) as alunosAleatorios
ORDER BY 'AlunoPontos' ASC

In this way, the internal query shuffles the table and returns only the first 5 results. Then, the external query causes the 5 results obtained to be sorted by the AlunoPontos column in order ASC .

Internal Query

SELECT * FROM 'aluno' ORDER BY RAND() LIMIT 0, 5

External Query

SELECT * FROM () as alunosAleatorios ORDER BY 'AlunoPontos' ASC
    
27.01.2016 / 17:12