SQL Server - 需要加入但不等于 [英] SQL Server - Need Join but where NOT Equal To

查看:44
本文介绍了SQL Server - 需要加入但不等于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 SQL Server 2012.

I am using SQL Server 2012.

我知道如何进行内部连接,在匹配的地方给出一个.我还需要获取不匹配的记录.

I know how to do an inner join which gives one where there is a match. I also need to get the records where there was not match.

最好的方法是什么.我想我可以做一个左连接和一个内连接,然后得到留下的那些.想知道最好和最干净的方法是什么.

What is the best approach. I guess I can do a left join and an inner join and then get the ones that are left behind. Wondering what the best and cleanest approach would be.

如前所述,我已经在进行内部联接,但还需要显示不匹配的记录.

As mentioned, I am already doing an inner join but also need to show records where there was no match.

推荐答案

您可能正在寻找外部联接或外部排除联接.

You probably are looking for an outer join or an outer excluding join.

外连接

SELECT *
FROM tableA a
FULL OUTER JOIN tableB b
    ON a.column = b.column

外部排除连接

SELECT *
FROM tableA a
FULL OUTER JOIN tableB b
    ON a.column = a.column
WHERE a.column IS NULL OR b.column IS NULL

此答案中的图表取自这篇非常有用的文章.

The graphs in this answer are taken from this very useful article.

这篇关于SQL Server - 需要加入但不等于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆