我有一个表,其中author1和author2列有类似(a,b)和(b,a)的镜像数据。但我不希望数据是多余的,因此需要一个SQL查询来消除(b,a)如果(a,b)已经存在的记录。

author1              author2                No of Publications
Anna Spagnolli       Luciano Gamberini           115
Luciano Gamberini    Anna Spagnolli              115
Anna Spagnolli       Giuseppe Riva                66
Giuseppe Riva        Anna Spagnolli               66
Giuseppe Riva        Luciano Gamberini            60
Luciano Gamberini    Giuseppe Riva                60
Achim Jung           Anna Spagnolli               33
Anna Spagnolli       Achim Jung                   33
Anna Spagnolli       John A. Waterworth           33
John A. Waterworth   Anna Spagnolli               33

对于EG,我不想要第二个记录,因为它已经存在于第一个记录中。

最佳答案

解决问题的方法:

SELECT table1.author1, table1.author2, table1.No_Of_Publications FROM table1
LEFT JOIN table1 as tbl
ON table1.author1 = tbl.author2
AND table1.author2 = tbl.author1
WHERE table1.author1 < tbl.author1
OR tbl.author1 IS NULL

演示:
http://sqlfiddle.com/#!9/2263fa/14

09-30 10:00