Okay, if you only have highest score per user per game that keeps it simpler.
This query:
Code:
/* highest scores in a given game */
SELECT S.score, U.username
FROM scores AS S, users AS U
WHERE S.userid = U.userid
AND S.gameid = 17 /*or any other gameid of course */
ORDER BY S.score DESC
LIMIT 10
Is used to show the highest 10 scores (change LIMIT 10 to LIMIT 25 to show highest 25, etc.) for a given game. A leaderboard for that game, in other words.
The "S" and "U" are called "aliases". You don't have to use them. You can write out the entire table name each time:
Code:
/* highest scores in a given game */
SELECT scores.score, users.username
FROM scores, users
WHERE scores.userid = users.userid
AND scores.gameid = 17 /*or any other gameid of course */
ORDER BY scores.score DESC
LIMIT 10
Note: If you do use an "AS" alias for a table name, then you *must* use that alias to refer to fields in the table. So all or nothing. I *always* use aliases, but that's me.
********
This query is WRONG!
Code:
SELECT 'score' from 'gamescores' where 'userid'="$id" AND 'gameid'="$gameid"
You can *NOT* use apostrophes around table or field names!
You *CAN* use backticks. The backtick ` usually is on the same keyboard key as the ~ tilde. BUT YOU DO NOT NEED TO USE backticks unless you foolishly give a name to a table or column that conflicts with a MySQL keyword or you use a name that uses invalid characters (e.g., spaces and minus signs, but many others).
You SHOULD NOT put quotes or apostrophes around NUMERIC values!
So that query, in PHP, would be correctly written as
Code:
$sql = "SELECT score FROM gamescores WHERE userid=$id AND gameid=$gameid";
(Because $id and $gameid *NEED* to be numbers in this query.)
**********
The
REFERENCES clause does *NOT* "access the columns from another table".
*IF* your tables are INNODB tables (instead of MYISAM tables), then a column that REFERENCES a column in another table does two things:
(1) It ensures that you can't use a value in the REFERENCES column that does not *already exist* in the primary table. For example, in the tables I showed, you would not be able to add a record to the
scores table for gameid 37 unless you already had a gameid 37 in the
games table.
(2) Once a record exists that has a value that correctly references another table, you can't delete the primary record until *all* references to it have been removed. In other words, once the
scores table has one or more records in it with userid 24, you won't be able to delete userid 24 from the
users table. You would need to first delete all records with userid 24 from the
scores table.
This is called "referential integrity". Look it up.
Again, this only applies to INNODB tables when using MySQL (it applies to all tables in most other DBs). *PROBABLY* all your tables will be INNODB tables. If in doubt, you can find out by using a data base query tool to issue a command such as
Code:
SHOW CREATE TABLE users;
And that will dump out the definition of the table including, near the end, what "engine" the table is using. For example (from my machine):
Code:
mysql> SHOW CREATE TABLE words;
CREATE TABLE `words` (
`word` varchar(30) DEFAULT NULL,
`wordcount` int(11) DEFAULT NULL,
UNIQUE KEY `ix2` (`word`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1