Alright, armed with the fact that two players participate in each game, I suggest consolidating the gamehistories
table with the gamescores
table:
- users uid, displayname, email
- games gid, pubID, start, end, uid1, score1, uid2, score2
This results in fewer joins required to retrieve the information you want, but adds one nuance - you don't know if your user id will be in the uid1
or uid2
column, so you have to query both.
SELECT users.displayname AS `Opponent Name`,
score1 AS `Your Score`,
score2.score AS `Opponent Score`,
TIMEDIFF(end, start) AS `Duration`
FROM
users INNER JOIN games ON users.uid = games.uid1
WHERE games.gid = (TheGID)
AND games.uid2 = (YourUID)
UNION
SELECT users.displayname AS `Opponent Name`,
score2 AS `Your Score`,
score1.score AS `Opponent Score`,
TIMEDIFF(end, start) AS `Duration`
FROM
users INNER JOIN games ON users.uid = games.uid2
WHERE games.gid = (TheGID)
AND games.uid1 = (YourUID)