I want to get the latest 5 messages in the inbox. To get the 5 latest IDs i need to use this:
SELECT
MAX(id)
FROM
samtaler
WHERE
brukerid_mottaker = 1
GROUP BY brukerid_avsender
ORDER BY id DESC
LIMIT 5
This return the correct ID's I need. But in the same query i want to select data from the same table, the row that got the id that returned from this query above.
I have tried out some things, variables and self-join but no luck:
select
p2.title,
p2.message,
@a:=max(p1.id)
from
samtaler p1
join samtaler p2
on (@a = p2.id)
where
p2.brukerid_mottaker = 1
group by p2.brukerid_avsender
order by p2.id DESC
limit 5
Why isnt this working?
This is the current data in the database:
I want to return in this case, row 13 and 4. Sorry for bad english.