Assume the game has two players. Each player has a score and highest score wins. A user table is self evident. The first design I thought of is as follows:
match_id, user1_id, user2_id, score1, score2
The problem is suppose I want a quick way of ranking users by win %. In the above table a single userid would be scattered between user1_id and user2_id. It would seem to require multiple queries and seems messy. The other way is to keep a separate stats table
user_id, winloss
This makes queries easy but doesn’t seem theoretically correct because the stats table stores derive data. Is there a way to solve this which involves a nice, clean theoretically correct data structure which is easy to query?
How about have one record for each user in a match? The primary key is then the combination of
match_idanduser_id. Also include a booleanwinnerfield.Though not part of your requirement, this also easily scales up to more players.
Querying win % is simply a matter of: