SELECT q.* FROM ( SELECT patient . * , visit.uid AS visit_uid FROM patient LEFT JOIN visit ON patient.uid = visit.patient_id ORDER BY visit_uid DESC ) q GROUP BY q.uid ORDER BY q.visit_uid DESC LIMIT 0 , 10