SQL Query: How to get items from one col paired with another but not vice versa

前端 未结 3 1649
死守一世寂寞
死守一世寂寞 2021-01-16 07:50

I need a query that returns all the rows from colA paired with colB but to treat the same values in the opposite direction as duplicates and to be removed.

The best

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-16 08:06

    If you prefer a "clean" SQL solution (without least() or greatest()) this also does your job:

    select colA, colB from your_table
    where colA > colB 
      or (colB, colA) not in (select colA, colB from your_table)
    

    SQL fiddle

提交回复
热议问题