本文介绍了如何比较SQL中的两列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有两个带有SSN变量的表,并且我想显示仅在一个表中的表,而不是两个表中的表.

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.

正确的方法是什么?

推荐答案

这是一种方法:

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.以下内容几乎可以在任何数据库中工作:

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

这篇关于如何比较SQL中的两列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 21:13