Mireille28 Mireille28 - 4 months ago 7
SQL Question

Get last record from each different id sql

hi actually im trying to reteive data from my table here is how it is build

id | room | user | date | comment
1 1 22 2289248 hello
2 2 33 2489298 thanks
3 1 17 2489300 ok
4 2 18 2490000 good


then what i try to retreive is the last entry made in each room based on the date then the result im looking for should look like this

3 | 1 | 17 | 2489300 | ok
4 | 2 | 18 | 2490000 | good


thanks for help with this

Answer

Order them first, then group them:

SELECT * FROM (SELECT * FROM rooms ORDER BY date DESC) t GROUP BY room

SQL fiddle

Comments