आप चाहते हैं:
ऐसा करने के लिए आपको समूह दर खंड में कुछ जादू करना होगा।
SELECT id, sender, receiver, MAX(time_sent), MAX(message)
FROM Messages
WHERE sender='$userid' OR receiver = '$userid'
Group By (if(sender > receiver, sender, receiver))
, (if(sender > receiver, receiver, sender))
Order BY time_sent DESC