SELECT DISTINCT
IF(IDMessaggioPadre=0,IDMessaggio,IDMessaggioPadre ) AS ID
FROM TABELLA
ORDER BY DATA DESC