如何在SQL中查找在一个表中但不在另一个表中的数据 [英] How do you find data that is in one table but is not in another in SQL

查看:135
本文介绍了如何在SQL中查找在一个表中但不在另一个表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,

我正在使用两个SQL表,一个是中间表",用于将数据从各个办公室导入到最终表".

我被要求做的是编写一个查询,该查询使用特定的标识字段来找出中间表"中的内容,而不是最终表"中的内容.

逻辑将类似于如果* field *在中间表"中并且最终表" = True.如果* field *在中间表"中但不在最终表"中'= False.如果为True =忽略,则为False =显示结果."

首先,我的逻辑对吗?

其次,我将使用什么SQL函数来实现这一目标?

Good morning,

I am working with two SQL tables, one is a sort of "middle table" that is used to import data from individual offices to a "final table".

What I have been asked to do is to write a query that uses a specific identifying field to find out what are in the "middle table" but are not in the "final table".

The logic would be something like, "If *field* is in ''middle table'' and ''final table''=True. If *field* is in ''middle table'' but not in ''final table''= False. If True=disregard, if False=display results."

First, is my logic right?

Second, what SQL function would I use to make this happen?

推荐答案

SELECT foo
FROM middle_table a LEFT OUTER JOIN final_table b
ON a.foo = b.foo
WHERE b.foo IS NULL



是从中间表而不是最终表中获取值的另一种方法



Is an alternative method for getting the values from the middle table not in the final table


没有SQL函数,仅提供代码

There is no SQL function for this, just code

SELECT foo
FROM middle_table
WHERE foo NOT IN(SELECT foo FROM final_table)


我倾向于使用 sql比较/ [
I tend to use sql compare/[^] for this kind of task - admittedly it''s not free, but it''s a darn good bit of kit.

[Disclaimer]I don''t and never have worked for Red Gate Software (I don''t even knowingly know anyone who does)


这篇关于如何在SQL中查找在一个表中但不在另一个表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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