Ho risolto:
codice:SELECT * FROM ( SELECT U.name as name,U.surname as surname,U.uid as uid,F.id_foto as id_foto,C.id as messageid,C.message as message,C.sent as sent FROM ".$_CONFIG['table_utenti']." U, ".$_CONFIG['table_foto']." F, ".$_CONFIG['table_chat']." C WHERE (C.from = U.uid OR C.to = U.uid) AND U.uid = F.uid AND U.uid != '".$_POST['uid']."' ORDER BY C.id DESC ) as my_table_tmp GROUP BY uid ORDER BY messageid DESC