select a.id,b.name name1,c.name name2 from 表A a left join 表B b
on a.NameID1=b.id left join 表B c on a.NameID2=c.id