Show the user his test position

Asked

Viewed 96 times

0

I have a table where you store test notes. Ex.:

Name, Nota1, Nota2, Nota3, Course

The result is this:

Nome  | Nota1 | Nota2 | Nota3 | Curso
Pedro | 7.6   | 5.5   | 3.2   | Desenho
João  | 8.0   | 3.1   | 6.6   | Desenho
Ana   | 9.0   | 6.5   | 2.2   | Desenho

I would like to verify that Ana took first, João in second and Pedro in third in the column Nota1 of the Drawing course. Thus:

Curso Desenho
1º lugar Ana
2º lugar João
3º lugar Pedro

I understand if I use it that way:

SELECT *, GREATEST(Nota1, Nota2, Nota3) AS MaiorNota FROM curso = '".$curso."' ORDER BY Nota1 DESC;

Or

 SELECT * FROM curso = '".$curso."' ORDER BY Nota1 DESC;

I can take the highest grade and put it in order from highest to lowest, but how can I show Pedro that he took 3rd place, Ana that she took 1st and so on?

3 answers

1


An alternative is to create a dynamic column with the position of each student within the ordering and then select it.

Considering a simplified table:

create table notas(
  id int not null auto_increment,
  nome varchar(255) not null,
  nota int,
  primary key (id)
);

You could do:

set @pos := 0;

select * from (
  select *, (@pos := @pos+1) as "posição"
  from notas
  order by nota desc
) as ranking
where ranking.nome = 'pedro'

Thus the select internal classify all students, generating the column posição. Then you make the selection from this result by filtering by the desired name. The result would be something like:

id  nome    nota    posição
1   pedro   76      2

That considering my test data:

insert into notas values (default, 'pedro', 76), 
                         (default, 'joão',  60), 
                         (default, 'ana',   92); 

See working on DB-Fiddle

  • Hello Anderson. I did exactly as you reported, changing of course, the nomenclatures. The table already existed and currently contains more than 8 thousand records and the note field is with the decimal value(7,2). The result of the position is returning as 4,283. Only when I give a query accounting for the applied subject, it returns only 9 students who did. In the example you gave me, I wrote the article in the WHERE clause. It was to return me from the 1st to the 9th, right? I also used the example applied in the link you suggested, but the result is the same.

  • You have the link of your example for me to reproduce the error?

  • Hello Anderson. Actually it is an intranet developed in PHP and Mysql client.

  • I say the example running on db-fiddle, as you say you did and returned wrong.

  • Oh yes... kkkk ... I did a test on DB-Fiddle and it worked inside our structure. I tried to paste here, but it exceeded the characters. I will give as I accept your answer, because I tried in another table and it worked tb. I believe it’s some problem from the table we’re using or something. Thank you for the strength!

0

I believe you can do it like this: select * from nome_tabela WHERE curso='Desenho' ORDER BY Nota1 DESC

In other words, the result of this select will be in descending order relative to the Nota1 field. You can also use at the end of this query a LIMIT 3 that will show only the first 3 results of the table, in case there are more records. Would look like this: select * from nome_tabela WHERE curso='Desenho' ORDER BY Nota1 DESC LIMIT 3

  • Hello Mboss. Right. I’ve tried this way, but he lists the grades from higher to lower. What I need to identify is that Pedro took 3rd place. The problem is that it does not limit up to 3rd place. It will show the user the position he took in the tests. Ex.: (5th, 7th, 11th, etc.). I know how to take the position of Pedro that I used as an example, it is easier to get the position of all users.

0

Good afternoon Fox, I will bring another point of view in question your question, as is marked the PHP tag I will assume that you are using PHP pro data processing/filtering.

Use the select of Mboss (I created a test comic)

SELECT * FROM tb_nota WHERE curso='desenho' ORDER BY vl_nota DESC

And in php, that’s where you’ll generate the positions: (Positions will be saved, according to each student)

$query = mysqli_query($connect,"select * from tb_nota WHERE nm_curso='desenho' ORDER BY vl_nota DESC;");
$posicao = 0;
while ($seq = mysqli_fetch_assoc($query)) {

    $posicao += 1;

    echo $seq['nm_aluno'] . " Posição : ".$posicao."° <br>";

}

The result would be like this:

inserir a descrição da imagem aqui

The data of the bank: (Remembering that I am displaying with the select done in DESC)

inserir a descrição da imagem aqui

I hope I’ve helped !!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.