I want to select 10 names from my table “games” for each game type.
for example: 10 rpg games, 10 action games, 10 MMO games…
The problem is that my query take up lot of resources, I am wondering if someone has the idea to write this query in a better way:
SELECT name
FROM games AS thisgame
WHERE (SELECT COUNT(1)
FROM games
WHERE games.game_id = thisgame.game_id
AND games.type > thisgame.type) <= 9
One way to do that fast is the variable trick. It relies on MySQL specific syntax to imitate a window function. For example, to retrieve up to two games per type:
Live example at SQL Fiddle.