I have two tables. One called movies
, and the other called movies_downloads
. Here is an Example of the Tables:
movies
:
╔═══╦════════════╗
║ ║ name ║
╠═══╬════════════╣
║ 1 ║ Movie1 ║
║ 2 ║ Movie2 ║
║ 3 ║ Movie3 ║
╚═══╩════════════╝
movies_downloads
:
╔═════════╦════════════╗
║ movieId ║ id (ai) ║
╠═════════╬════════════╣
║ 1 ║ 1 ║
║ 1 ║ 2 ║
║ 2 ║ 3 ║
║ 3 ║ 4 ║
╚═════════╩════════════╝
Like you can see, it might be two rows in movies_downloads
of the same movie. (in this case, it's movie #1)
I want to select all of the movies from the end to the begining, and I want to join the downloads table, and take the last row (biggest id) from it.
In this case, the result will be:
MovieId Name movies_downloads.id
3 Movie3 4
2 Movie2 3
1 Movie1 2
The problem is: that instead of giving me the id#2
(beacuse he's the last), it gives the id#1
. (which is the first)
My Code:
SELECT `movies`.*, `movies_downloads`.`id`
FROM `movies`
JOIN `movies_downloads`
ON `movies`.`id` = `movies_downloads`.`movieId`
GROUP BY `movies`.`id`
ORDER BY `movies_downloads`.`id`
DESC
LIMIT 10
BTW, I use mysql engine, if it matters.
Thank you!