紧急求助!!如何联合查询出两张表之间不同的数据?请高手指教

Bon_soir 2011-07-19 03:24:40
如何联合查询出两张表之间不同的数据,例如我有两张表:table1和table2,它们关联的字段有的firstname,lastname,address.我如何根据firstname,lastname,address联合查找出两张表不同的数据?请高手指教 ,谢谢..
...全文
453 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangyong1107 2011-07-19
  • 打赏
  • 举报
回复

select firstname,lastname,address from table2 except
(
select firstname,lastname,address from table1
)
liangyong1107 2011-07-19
  • 打赏
  • 举报
回复
except
claro 2011-07-19
  • 打赏
  • 举报
回复
建议用tablediff ,可以生成SQL数据。
http://msdn.microsoft.com/zh-cn/library/ms162843.aspx

C:\Program Files\Microsoft SQL Server\100\COM>tablediff /?
Microsoft (R) SQL Server Replication Diff Tool
Copyright (c) 2008 Microsoft Corporation

User-specified agent parameter values:
/?

Replication Diff Tool Command Line Options

usage: tablediff

-- Source Options --
-sourceserver Source Host
-sourcedatabase Source Database
-sourceschema Source Schema Name
-sourcetable Source Table or View
-sourceuser Source Login
-sourcepassword Source Password
-sourcelocked Lock the source table/view durring tablediff

-- Destination Options --
-destinationserver Destination Host
-destinationdatabase Destination Database
-destinationschema Destination Schema Name
-destinationtable Destination Table or View
-destinationuser Destination Login
-destinationpassword Destination Password
-destinationlocked Lock the destination table/view durring tablediff

-- Misc Options --
-t Timeout
-c Column Level Diff
-f Generate Fix SQL (You may also specify a file name and path)
-q Quick Row Count
-et Specify a table to put the errors into
-dt Drop the error table if it exists
-o Output file
-b Number of bytes to read for blob data types
-strict Strict compare of source and destination schema
-rc Number of retries
-ri Retry interval
cd731107 2011-07-19
  • 打赏
  • 举报
回复
请楼主注意一下自己结贴率

--table2中与table1不匹配的记录
select * from table1 a
where not exists (select 1 from table2 b where a.firstname=b.firstname
and a.lastname=b.lastname and a.address=b.address)

--再查table1中与table2不匹配的记录
select * from table2 b
where not exists (select 1 from table1 a where a.firstname=b.firstname
and a.lastname=b.lastname and a.address=b.address)
spiritofdragon 2011-07-19
  • 打赏
  • 举报
回复
先查 连得上的:inner join 。 再not in 这个子查询。
--小F-- 2011-07-19
  • 打赏
  • 举报
回复
select * from t1 checksum(firstname,lastname,address) not in(select checksum(firstname,lastname,address) from t2)
Bon_soir 2011-07-19
  • 打赏
  • 举报
回复
就是两张表之间firstname,lastname,address不同的就行了...
快溜 2011-07-19
  • 打赏
  • 举报
回复
两张表结构不同?
P1mm 2011-07-19
  • 打赏
  • 举报
回复
两张表不同的数据如何定义?

说明定义了写语句是很简单的

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧