日期:2014-05-19  浏览次数:21006 次

2个结构相同的表如何查找不同的数据
表a字段名为email
表b字段名也是email

a表的数据包括了b表中的内容
我现在想要得到b表在a表中没有的数据

说的清楚吗?反正就是一个简单的sql,可是我不太会


------解决方案--------------------
--用chencksum()函数

select *
from B
where checksum(*) not in (select checksum(*) from A)

------解决方案--------------------
select * from b where 主键 not in (select 主键 from a)

select * from a where 主键 not in (select 主键 from b)
------解决方案--------------------
CHECKSUM
返回在表的行上或在表达式列表上计算的校验值。CHECKSUM 用于生成哈希索引。

语法
CHECKSUM ( * | expression [ ,...n ] )

参数
*

指定在表的所有列上进行计算。如果有任一列是非可比数据类型,则 CHECKSUM 返回错误。不可比数据类型是 text、ntext、image、cursor 以及基本类型为前 4 个数据类型之一的 sql_variant。

expression

是除非可比数据类型之外的任何类型的表达式。

返回类型
int

---------
学习
------解决方案--------------------
--查找表结构相同的重复数据

select *
from B
where checksum(*) in (select checksum(*) from A)

union

select *
from A
where checksum(*) in (select checksum(*) from B)

------解决方案--------------------
--其实用一个就可以

--查找表结构相同的重复数据

select *
from B
where checksum(*) in (select checksum(*) from A)