Използвайте MAX -оператор и група по UID . SELECT MAX(t.ID), t.UID FROM yourtable AS t GROUP BY t.UID;