I have a game table called game
, a game platform table called plataformas
and a table that makes the N-to-N relationship between the two calls game_plataforma
.
I have a query that has to search for a gaming platform, but it has to sort according to the last game that was registered in it.
I have the following query:
select DISTINCT(p.id), p.slug, p.nome,gp.id_plataforma
from game g
join game_plataforma gp on g.id=gp.id_game
join plataformas p on gp.id_plataforma=p.id
where g.ativo='1'
group by p.nome
order by g.dia DESC
limit 4
Only you are not ordering correctly. It is sorting alphabetically by platform name.
I did the following test:
select id_plataforma from game_plataforma order by id DESC;
And it returned right these values down to the field id_plataforma
:
2, 22, 2, 25, 2, 2, 2, 2, 25,2,1,2,26,6,6,22,2,22-
Now when I run this one:
select distinct(id_plataforma) from game_plataforma order by id DESC;
It returns this here to the same fields:
25, 1, 23, 6, 2, 22
And with group by
give this:
select id_plataforma from game_plataforma group by id_plataforma order by id DESC;
25, 1, 23, 6, 2, 22
Am I going crazy or is there something wrong?
Structure of the game_plataforma
table:
What I need to come up with are the platform ids in this order:
2, 22, 25, 1, 23