使用SQL比较两个列,以确保两个列中存在相同的值集
我有以下几列要比较。需要确保表1中代码列中的所有值都存在于表2中的代码列中。两者都有以不同顺序存储的代码。需要循环并对照其他表中的每一行值检查每一行值 表1使用SQL比较两个列,以确保两个列中存在相同的值集,sql,Sql,我有以下几列要比较。需要确保表1中代码列中的所有值都存在于表2中的代码列中。两者都有以不同顺序存储的代码。需要循环并对照其他表中的每一行值检查每一行值 表1 Id Code 1 2 1 3 1 1 1 4 表2 Id Code 1 1 1 2 1 3 1 4 减号函数将给出两个语句之间的差异: select id, code from table_1 minus select id, code from table_2 简单地将两者结合起
Id Code
1 2
1 3
1 1
1 4
表2
Id Code
1 1
1 2
1 3
1 4
减号函数将给出两个语句之间的差异:
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)
使用
不存在
获取缺少的代码:
select t1.*
from table1 t1
where not exists (select 1
from table2 t2
where t2.id = t1.id and t2.code = t1.code
);
如果ID不同怎么办?您希望得到什么结果?在SQL Server中,这将是“除外”。RDBMS需要标记。