SQL 比较两个不同表中的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15705244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:32:34  来源:igfitidea点击:

Compare a column in two different tables

sqloracle

提问by ad2387

Say I have two tables, Table A and Table B, and I want to compare a certain column.

假设我有两个表,表 A 和表 B,我想比较某个列。

For example,

例如,

Table A has the columns: IP,Host,App

表 A 包含以下列:IP、Host、App

Table B has the columns: IP,Datacenter,Server,Model,Last_Updated

表 B 有以下列:IP,Datacenter,Server,Model,Last_Updated

How do I compare the IP column between the two tables to get the differences?

如何比较两个表之间的 IP 列以获取差异?

I know if the tables have the same columns I can use a union and 'minus' to get the differences but I wasn't able to figure out a way if the tables have different columns.

我知道如果表具有相同的列,我可以使用联合和“减号”来获取差异,但是如果表具有不同的列,我无法找出一种方法。

Thanks!

谢谢!

回答by Quassnoi

SELECT  *
FROM    A
FULL JOIN
        B
ON      a.IP = b.IP
WHERE   a.IP IS NULL OR b.IP IS NULL

This will output all columns from non-matching rows in both tables, with NULLs on either side.

这将输出两个表中不匹配行的所有列,两边都有 NULL。

回答by Nitin

select distinct column_A FROM table_1 where column_A not in (SELECT column_A FROM table_2)

回答by Brian

you mean you want to get all IPs in table A that are not in table B?

您的意思是要获取表 A 中不在表 B 中的所有 IP?

select IP from table A
MINUS
select IP from table B

Did I understand the question correctly?

我是否正确理解了问题?