I have a database with a table called 'messages'. The colums are 'sender', 'reseiver', 'subject', 'time' (the timestamp).
With my query, I want to get the entries grouped by the subject. BUT I want to have the corresponding time to be the latest.
So, for example, if this was my db:
person1 | person2 | hello | 2019-06-24 20:0**7**:00
person2 | person1 | hello | 2019-06-24 20:0**8**:00
I want to get this:
subject: hello
last entry: 2019-06-24 20:0**8**:00
I wasn't able to put the ORDER BY in front of the GROUP BY, so I tried a subquery:
SELECT subject, sender, receiver, time
FROM
(SELECT subject, sender, receiver, time
FROM messages
ORDER BY time DESC) AS subselect
GROUP BY subject
But it doesn't work and I can't figure out why.
Can somebody help me?