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
yang Anda butuhkan adalah id percakapan unik antara mitra obrolan. Saya telah mensimulasikan ini dengan subquery, semoga membantu