SELECT messages.* FROM messages, (SELECT MAX(id) as lastid FROM messages
WHERE receiver = 13 OR sender = 13
GROUP BY CONCAT(LEAST(receiver,sender),'.',GREATEST(receiver,sender))) as conversations
WHERE id = conversations.lastid
ORDER BY timestamp DESC
co potřebujete, je jedinečné ID konverzace mezi partnery chatu. Simuloval jsem to pomocí dílčího dotazu, doufám, že to pomůže