I have the following table:
CREATE TABLE `score` ( `score_id` int(10) unsigned NOT NULL auto_increment, `user_id` int(10) unsigned NOT NULL, `game_id` int(10) unsigned NOT NULL, `thescore` bigint(20) unsigned NOT NULL, `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP, PRIMARY KEY (`score_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
That’s a score table the stores the user_id and game_id and score of each game. there are trophies for the first 3 places of each game. I have a user_id and I would like to check if that specific user got any trophies from any of the games.
Can I somehow create this query without creating a temporary table ?
This query returns the rows for all winning games. Although ties are included; if the scores are 10,16,16,16,18 then there are four winners: 16,16,16,18. I’m not sure how you handle that. You need some way to resolve ties in the join condition.
For example, if ties are resolved by the earlier game winning, then you could modify the query this way:
You could also use the
timestampcolumn to resolve ties, if you can depend on it beingUNIQUE.However, MySQL tends to create a temporary table for this kind of query anyway. Here’s the output of
EXPLAINfor this query: