I create this SQL query:
SELECT * FROM favorite_user_cellphone fc
WHERE user_id = 123 AND unfavorited_at is null
ORDER BY favorited_at DESC;
the result of this query is below:
favorite_id | user_id | cellphone_id | favorited_at | unfavorited_at
7 123 1225 2019-04-13 12:35:02 null
5 123 1225 2019-04-11 12:35:02 null
2 123 1275 2019-04-09 13:00:02 null
1 123 1225 2019-04-09 12:35:02 null
But now I would like to remove the duplicate rows (column cellphone_id) and keep the row with the most current date for each cellphone_id
So, the final result should be like this:
favorite_id | user_id | cellphone_id | favorited_at | unfavorited_at
7 123 1225 2019-04-13 12:35:02 null
2 123 1275 2019-04-09 13:00:02 null
I do it:
SELECT * FROM favorite_user_cellphone fc
WHERE user_id = 123 AND unfavorited_at is null
ORDER BY favorited_at DESC;