How do I compare two columns in SQL?

前端 未结 1 1788
无人共我
无人共我 2021-01-29 03:04

Lets say I have a two table both with a SSN variable, and I want to show the ones that are in only one table, not both.

What is the correct way to do this?

相关标签:
1条回答
  • 2021-01-29 03:57

    Here is one way:

    select coalesce(t1.ssn, t2.ssn)
    from t1 full outer join
         t2
         on t1.ssn = t2.ssn
    where t1.ssn is null or t2.ssn is null;
    

    This works in most databases, but not in MySQL. The following should work in pretty much any database:

    select ssn
    from ((select ssn, 't1' as which
           from t1
          ) union all
          (select ssn, 't2' as which
           from t2
          )
         ) t
    group by ssn
    having count(distinct which) = 1
    
    0 讨论(0)
提交回复
热议问题