SELECT t1.id, t1.name, t2.uid FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.tid AND t2.uid = 22 ORDER BY t2.uid DESC, t1.name