I have a status Database like the following:
id foreign_id status
1 1 1
2 5 2
3 2 0
4 5 4
This Database relates some objects (foreign_id) with some status. My Aim is now to get for all the foreign_ids the last status. Unfortunately if I use DISTINCT on foreign_id my mysql-Database choose by random a status, not the latest. Even if I order by id DESC it doesn't work. How can I manage to get my Information?
In this example my desired output would be:
id foreign_id status
1 1 1
3 2 0
4 5 4
Note: The second line is missing, because there is a newer line (4) for the foreign_id "5".
I already tried a GROUP BY construction, but it fails the same way, DISTINCT does.