|
Your create table looks good, but I'd recommend that you don't call your time column "time", but instead "gametime" or something like that. time is reserved word and can cause you problems. Also, you may want to call the table gamescores or something like that, just so your table and column aren't named the same. If you want to have the columns indexed properly, you can do something like this when you create the table.
CREATE TABLE gamescores (
id INT AUTO_INCREMENT, PRIMARY KEY(id),
gameid INT NOT NULL REFERENCES game (id),
playerid INT NOT NULL REFERENCES player (id),
score INT NOT NULL,
gametime DATETIME,
INDEX(score),
INDEX(gameid),
INDEX(playerid)
);
I assume you'll be searching on all of those columns. You may even want to index gametime if you'll be using that in queries. Also, I don't use the "REFERENCES" very often with MySQL, so I'm not sure about the syntax on that part of the create. Depending on where you want the data control, you could just take the REFERENCES out.
As for the SELECT:
SELECT playerid,score FROM gamescores WHERE gameid = n ORDER BY score DESC LIMIT 10
That will give you the top 10 scores (assuming that the highest score is the best)
Hope that helps!
|