My table contains votes of users for different items. It has the following fields:
id, user_id, item_id, vote, utc_time
I understand how to get the last vote of #user# for #item#, but it uses subquery:
SELECT votes.*, items.name, items.price
FROM votes JOIN items ON items.id = votes.item_id
WHERE user_id = #user# AND item_id = #item#
AND utc_time = (
SELECT MAX(utc_time) FROM votes
WHERE user_id = #user# AND item_id = #item#
)
It works, but it looks quite stupid to me... There should be a more elegant way to get this one record. I tried the approach suggested here, but I cannot make it work yet, so I'll appreciate your help: How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?
There is a second part to this question: Count rows with DISTINCT(several columns) and MAX(another column)