SQL Server выберите игнорировать Dupplicates
SELECT DISTINCT(user_id) FROM messages ORDER BY id DESC LIMIT 5
Gifted Gibbon
SELECT DISTINCT(user_id) FROM messages ORDER BY id DESC LIMIT 5