I have a MySQL table which looks like the following:
user_id resource_id last_used
--------------------------------
1 1 2010-09-01
1 2 2010-09-02
1 3 2010-09-04
2 3 2010-09-08
2 2 2010-09-03
3 1 2010-09-01
3 1 2010-09-05
I need to write a query which tells me which resource was used last by which user, with each user and each resource only listed at most once (it's okay if some users and some resources aren't listed). In the above case, I'd like the following result set:
user_id resource_id last_used
--------------------------------
2 3 2010-09-08
3 1 2010-09-05
Note that each user id appears at most once, each resource id appears at most once, and preference is given to the most recent last_used date. It's okay for my purposes that user 1
and resource 2
don't appear at all (although it would be also be fine if 1 2 2010-09-02
appeared in the results). This query needs to run on a potentially large database (500,000+ rows). Is there an easy, reasonably fast query to do this in SQL?