Fetch the last messages in each group by the attribute name
SELECT m1.*
FROM messages m1 LEFT JOIN messages m2
ON (m1.name = m2.name AND m1.id < m2.id)
WHERE m2.id IS NULL;
SELECT *
FROM messages where id in (
select max(id) from messages group by name
);