1.Purchase table
Id user pur_type
----------------------
1 408 5-12
2 408 5-12
3 222 1-11
4 222 5-12
5 408 1-11
6 408 1-11
2. goods table
Id parts days
-----------------
1 1-11 50
2 5-12 40
I implemented such query
SELECT p1.id, p1.user, p1.pur_type, g.parts, g.days
FROM purchase p1
INNER JOIN goods g ON p1.pur_type = g.parts
LEFT JOIN goods p2 ON ( p1.pur_type= p2.pur_type
AND p1.id < p2.id )
WHERE p2.id IS NULL
The result is only last record for each pur_type
Id user pur_type parts days
---------------------------------
4 222 5-12 5-12 40
6 408 1-11 1-11 50
How to get last record from purchase
table of each pur_type
for one specific user?
for example for user 408 need result:
Id user parts days
-------------------------
2 408 5-12 40
6 408 1-11 50
Result explanation:
last record fot part
5-12 for user 408 is id
=2
last record for part
1-11 for user 408 is id
=6