Removing Mirrored Pairs from SQL Join

前端 未结 3 575
遥遥无期
遥遥无期 2021-02-10 19:40

I have a table with 2 fields (name, interest) and I want to find all pairs that have the same interest, with all duplicates and mirrored pairs removed.

I am able to find

3条回答
  •  青春惊慌失措
    2021-02-10 20:13

    Suppose we have table Name with tuples:

     F1  F2           
    Jon Smith          
    Smith Jon
    

    then to remove this pair we can make query like this:

    SELECT n1.F1, n1.F2         
    FROM Name n1            
    WHERE n1.F1 > (SELECT n2.F1  
                      FROM Name n2  
                      WHERE n1.F1=n2.F2)
    

    So Instead of using <> in

    (SELECT * FROM Matches
    WHERE name2 **<>** (select name1 from Matches);)
    

    use > or < operator and It should work fine.

提交回复
热议问题