Reputation: 1546
Having the following two tables:
Players
id | player
-----------
1 | ABC
2 | CDE
3 | FGH
Games
id | player_id | created_at
-------------------------------------
1 | 1 | 2019-09-01 15:00:00
2 | 1 | 2019-09-15 17:00:00
3 | 2 | 2019-10-01 15:00:00
4 | 2 | 2019-10-05 18:00:00
5 | 2 | 2019-10-12 15:00:00
How can I select all players and show their latest if they have any, or NULL if they never played a game? Something like this with this example:
player_id | created_at
--------------------------------
1 | 2019-09-15 17:00:00
2 | 2019-10-12 15:00:00
3 | NULL
Upvotes: 2
Views: 323
Reputation: 37473
You can try below - use left join
select p.id, max(created_at)
from Players p left join Games g on p.id=g.player_id
group by p.id
Upvotes: 3