[英]comparing two tables to make sure they are same row by row and column by column on SQl server
[英]Comparing two columns using SQL to make sure the same set of values are present in both
我有以下幾列要比較。 需要確保表 1 的代碼列中的所有值都存在於表 2 的代碼列中。兩者的代碼存儲順序不同。 需要針對其他表中的每個行值循環並檢查每個行值。
表格1
Id Code
1 2
1 3
1 1
1 4
表 2
Id Code
1 1
1 2
1 3
1 4
減去 function 會給你兩個語句之間的區別:
select id, code
from table_1
minus
select id, code
from table_2
只需加入兩者
Select * from table1 t1 join table2 t2
On t1.id=t2.id and t1.code=t2.code
或使用存在/在
Select * from table1 t1
where exists(
Select 1 from table2 t2 where
t1.id=t2.id and t1.code=t2.code)
Select * from table1 t1
where id, code
In (
Select id, code from table2)
使用not exists
獲取缺失的代碼:
select t1.*
from table1 t1
where not exists (select 1
from table2 t2
where t2.id = t1.id and t2.code = t1.code
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.