select a.id as id,
(select name from table2 where id = a.tableid1) as id1,
(select name from table2 where id = a.tableid2) as id2
form table1 a