如何比较SQL中的两列?

时间:2013-07-09 02:42:18

标签: mysql sql

假设我有一个带有SSN变量的两个表,我想显示仅在一个表中的表,而不是两个表。

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

这是一种方式:

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;

这适用于大多数数据库,但不适用于MySQL。以下几乎适用于任何数据库:

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