借用一下楼上的思路,稍微优化了一下 select p1, p3 as p2 from (select t1.p1 p1,t1.p2 p2,t2.p1 p3,t2.p2 p4 from friend t1 left join friend t2 on t1.p2=t2.p2 and t1.p1!=t2.p1 union select t3.p2,t3.p1,t4.p2,t4.p1 from friend t3 left join friend t4 on t3.p1=t4.p1 and t3.p2!=t4.p2 ) t5 where p1<p3;