[英]How to compare between two tables?
this is my query这是我的查询
select * from table as a
where a.* not in
(select * from table B)
I want to have the difference beteween two tables there is a specific function?我想有两个表之间的差异有一个特定的function?
By definition, EXCEPT returns distinct rows by comparing the results of two queries.根据定义,EXCEPT 通过比较两个查询的结果返回不同的行。
EXCEPT returns distinct rows from the left input query that aren't output by the right input query. EXCEPT 从左输入查询返回不同的行,这些行不是右输入查询的 output。
The basic rules are:基本规则是:
CREATE TABLE MyTableA (ColA int, ColB int) CREATE TABLE MyTableB (ColA int, ColB int) INSERT INTO MyTableA (ColA, ColB) VALUES (15,1),(10,1),(2,1),(2,1),(16,1),(2,2),(3,3),(3,3) INSERT INTO MyTableB (ColA, ColB) VALUES (1,1),(1,1),(1,1),(2,2),(4,5),(1,1),(4,5)
GO GO
SELECT * FROM MyTableA EXCEPT SELECT * FROM MyTableB Select * from MyTableA as a where not exists (Select 1 from MyTableB as b where a.ColA = b.ColA and a.ColB = b.ColB) GO
ColA |科尔A | ColB ---: | ColB ---: | ---: 2 | ---: 2 | 1 3 | 1 3 | 3 10 | 3 10 | 1 15 | 1 15 | 1 16 | 1 16 | 1 ColA | 1 可乐 | ColB ---: | ColB ---: | ---: 15 | ---: 15 | 1 10 | 1 10 | 1 2 | 1 2 | 1 2 | 1 2 | 1 16 | 1 16 | 1 3 | 1 3 | 3 3 | 3 3 | 3 3
You can see that using EXCEPT generated duplicate entries, if you want to get rid of that you may need an ID column to both tables and update your query to:您可以看到使用 EXCEPT 生成的重复条目,如果您想摆脱它,您可能需要两个表的 ID 列并将查询更新为:
Select *
from MyTableA as a where not exists (Select 1 from MyTableB as b
where a.ColA = b.ColA and a.ColB = b.ColB and a.ID <> b.ID)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.