Remove reverse duplicates from an SQL query

前端 未结 2 399
囚心锁ツ
囚心锁ツ 2020-12-05 15:06

Let\'s say that a query result is supposed to return a list of string pairs (x, y). I am trying to eliminate the reverse duplicates. What I mean is, if (x, y) was one of the

相关标签:
2条回答
  • 2020-12-05 15:53

    First of all, welcome to 2012. We have migrated away from relating tables using commas. It was introdued in ANSI 89 but is severely lacking. Nowaways, the correct way is to write queries using the ANSI 92/99/2003 JOIN syntax.

    The solution to your problem is to turn your bidirectional inequality <> into a unidirectional inequality, either < or > whichever you prefer.

    select e.column3, f.column3
    from example as e
    join example as f on e.column2 = f.column2 and e.column3 < f.column3
    
    0 讨论(0)
  • 2020-12-05 15:57
    select e.column3, f.column3 from example as e, example as f where e.column2 = f.column2
           and e.column3 <> f.column3 where e.id < f.id
    

    adding a simple where clause should do it.

    0 讨论(0)
提交回复
热议问题