My table structure is as follows:
mid | tid | data
| 1 | 0 | data
| 1 | 1 | data
| 1 | 2 | data
| 1 | 3 | data
| 1 | 4 | data
| 2 | 0 | data
| 2 | 1 | data
| 3 | 0 | data
| 3 | 1 | data
| 3 | 2 | data
| 4 | 0 | data
.
.
.
| N | 0 | data
| N | 1 | data
| N | 2 | data
.
.
| N | K | data
Each record is identified by 2 indices, a mid and tid. The tid will be unique to each mid. For each mid the tid starts at 0 and counts upwards to K. There can be imminently many tids for a given mid, and there can be infinitely many mids.
What I want to do is select the 3 most recent MIDs and their corresponding most recent tids WHERE the data meets some criteria (say > 1). If only MIDs 1, 2, 3, and 4 met the data requirements I would want it to return (omitting 1 since it is not the 3 most recent but rather the 4th):
| 2 | 1 | data
| 3 | 2 | data
| 4 | 0 | data
I am lost on how to do this, any help would be appreciated. A dual order by LIMIT 3 would return
| 3 | 1 | data
| 3 | 2 | data
| 4 | 0 | data
JOINagainst a subquery which gets theMAX(tid)for eachmid:Update: Added the
ORDER BY&LIMITclause, which I missed in the OP.