有这样一个表relation(f1, f2, c), 要去查询表中记录,并且将不同记录中f1 = f2, f2 = f1, c = c的结果去重,比如(a, b, c), (b, a, c)只保留一条记录。
答案参考下面:
select * from relation where (f1, f2, c) not in (select a.f1, a.f2, a.c from relation as a inner join relation as b on a.f1 = b.f2 and a.f2 = b.f1 and a.c = b.c where a.f1 > a.f2);
这里面没有考虑a.f1 = a.f2, 加上这个条件,只需增加distinct:
select distinct * from relation where (f1, f2, c) not in (select a.f1, a.f2, a.c from relation as a inner join relation as b on a.f1 = b.f2 and a.f2 = b.f1 and a.c = b.c where a.f1 > a.f2);