select A.外键 from (select * from 表 where 字段B=1) as A, (select * from 表 where 字段B=2) as B where A.外键=B.外键