I have a table msg(ID,TO,FROM,MSG,TIME) and I need to select distinct tuples from TO and FROM and order the result based on the latest time a message has been sent. The queries I have used so for
SELECT DISTINCT value 
FROM ( SELECT msg.to AS value, time 
        FROM msg 
        WHERE msg.from = '".$_SESSION["username"]."' 
    UNION 
        SELECT msg.from AS value, time 
        FROM msg 
        WHERE msg.to = '".$_SESSION["username"]."') TT 
order by time desc
And
select t1.to, t1.time 
from msg t1 
where t1.from='".$_SESSION["username"]."' 
    and t1.time=(
        select max(time) 
        from msg t2 
        where t2.to=t1.to) 
union 
select t1.from, t1.time 
from msg t1 
where t1.to='".$_SESSION["username"]."' 
    and t1.time=(
        select max(time) 
        from msg t2 
        where t2.from=t1.from)
But none give the desried result.
 
     
     
    