select * from tA left join tB on tA.id = tB.id and tA.flag = 1 where tB.code = 2 select * from tA left join tB on tA.id = tB.id where tB.code = 2 and tA.flag = 1