Show the three best rated vehicles from a table

Asked

Viewed 24 times

1

I am making a website for the course, but I have a problem. I chose to use two tables, one for "ratings", and one for "vehicles". I would then like to select the 3 best rated vehicles from the table, and that it be shown on the main page.

Table "classifications": https://i.stack.Imgur.com/Sdriq.png

Table "vehicles": https://i.stack.Imgur.com/Fwxxq.png

Note: The id of the table 'classificacoes' is the same id of the other table.

  • who will be evaluated most?

2 answers

1

forehead there:

select vec.nome, classic.nome from veiculos as vec
inner join classificacoes as classic
on classic.id_veiculos = vec.id
where classic.estrelas > 3
order by classic.estrelas asc

Unfortunately I’m not being able to access my database at the moment, but this should do the trick. Leading to the assumption that the relevant number of stars is greater than 3.

  • this query will return the vehicles with more than 3 stars and this is not what he asked.

0

For the consultation return only the 3 vehicles best evaluated:

select * from classificacoes order by estrelas desc limit 3

Using Join between tables, it would be like this:

select * from classificacoes c, veiculos v
where c.id_veiculo = v.id
order by c.estrelas desc limit 3

http://sqlfiddle.com/#! 9/05dbb2/1

Browser other questions tagged

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