Answer the question
In order to leave comments, you need to log in
How to compose a query so that the largest value is selected when grouping?
Hi all. For a day now I have been trying to figure out a seemingly simple request, but the couple does not work.
The request itself:
SELECT meta.meta_value, game.title FROM meta
INNER JOIN game ON game.id = meta.id_game
WHERE game.month = 2 AND game.year = 2016 AND meta.meta_key = 'video'
GROUP BY meta.id_games
ORDER BY game.score + game.like DESC LIMIT 10
game meta
id id
title id_games
month meta_key
year meta_value
score
like
Answer the question
In order to leave comments, you need to log in
SELECT MAX(meta.meta_value), game.title
FROM meta
INNER JOIN base ON game.id = meta.id_game
WHERE game.month = 2 AND game.year = 2016 AND meta.meta_key = 'video'
GROUP BY meta.id_games
ORDER BY game.score DESC, game.like DESC
LIMIT 10
Something like this:
select meta.meta_value, game.title
from game
join (SELECT game.id as id_games, max(meta.id) as id_meta
FROM meta
JOIN game ON game.id = meta.id_game and game.month = 2 AND game.year = 2016
WHERE meta.meta_key = 'video'
GROUP BY game.id
ORDER BY game.score + game.like DESC LIMIT 10) A on A.id_games=game.id
join meta on meta.id=A.id_meta
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question