Well, armed with two players participating in each game, I propose to consolidate the gamehistories table with the gamescores table:
- users uid, displayname, email
- games gid, pubID, start, end, uid1, score1, uid2, score2
This leads to fewer connections required to obtain the required information, but adds one nuance - you do not know if your user ID will be in the uid1 or uid2 , so you need to request 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)
source share