I have a simple database that keeps track of transactions from multiple players in a game.
When a game is started between two players, a unique game_id is generated and stored in a separate database called game_ids. In the database I am querying (game_transactions) I need the following to happen…
Table game_transactions sample data:
game_id | home_id | visiting_id | timestamp | ...
3 1 2 00000001
4 1 3 00000001
3 2 1 00000002
3 1 2 00000003
4 1 3 00000002
3 2 1 00000004
4 1 3 00000003
As you can see, when one player completes a turn their game is added to this table. This goes on until the game is completed. What I am trying to do is get all rows when searching by the user_id “WHERE home_id = user_id OR visiting_id = user_id”. However this returns ALL the rows for that specific user, I want to limit the query to give the row of data for the MOST RECENT game_id by the given timestamp. The timestamp is an actual UNIX time stamp, but for example purposes this is more readable.
When said and done, this query needs to return the following when user_id = 1:
game_id | home_id | visiting_id | timestamp | ...
3 2 1 00000004
4 1 3 00000003
Please let me know if I need to clarify further.
Thank you.
On a side note, I will be purging the database of entries older than x days. But this will be a cron that runs once a day, trying to keep queries to a minimum!
1 Answer