Jeg tror, det er det, du leder efter.
SELECT messages.*
FROM
(SELECT MAX(lastseen) AS lastseen,IF ('Tom' = `from`,`to`,`from`) as otheruser FROM messages
WHERE 'Tom' IN (`from`,`to`) GROUP BY otheruser
)
AS latest INNER JOIN messages ON latest.lastseen = messages.lastseen
AND (('Tom' = messages.from AND latest.otheruser = messages.to)
OR
('Tom' = messages.to AND latest.otheruser = messages.from))
ORDER BY messages.lastseen
DESC,`read`='no' limit 10
bare erstatte 'Tom' med din variabel
Dette vil returnere de seneste 10 brugere, der har sendt en besked til 'Tom' eller 'Tom'.