数据表内容如何比较出差异??

HUANG_JH 2004-06-21 12:42:54
有两个数据库,表结构一样,绝大部分的数据相同,但是有不部分数据不同,如何查出这部分数据??

例如
数据库A
表a字段
ID p1 p2 p3 p4
11 11 12 13 14
22 21 22 23 24
33 31 32 33 34
44 41 42 43 44

数据库B
表a字段
ID p1 p2 p3 p4
11 11 12 13 14
22 21 22 23 24
33 3 3 33 3
44 41 42 43 44

以 数据库A为标准,把数据库B中33 这条记录查出来
...全文
159 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
netcoder 2004-06-21
  • 打赏
  • 举报
回复
人家说的是两个库呀
select B.* from 库A..a A join 库B..a B ON A.ID=B.ID AND (A.p1<>B.p1 OR A.p2<>B.p2 OR A.p3<>B.p3 OR A.p4<>B.p4)
zjcxc 2004-06-21
  • 打赏
  • 举报
回复
--或者:
select * from 表B b
where exists(
select 1 from 表A
where id=b.id and(
p2<>b.p2
or p3<>b.p3
or p4<>b.p4))
zjcxc 2004-06-21
  • 打赏
  • 举报
回复
--测试

--测试数据
create table 表a(ID int,p1 int,p2 int,p3 int,p4 int)
insert 表a select 11,11,12,13,14
union all select 22,21,22,23,24
union all select 33,31,32,33,34
union all select 44,41,42,43,44

create table 表b(ID int,p1 int,p2 int,p3 int,p4 int)
insert 表b select 11,11,12,13,14
union all select 22,21,22,23,24
union all select 33,3, 3, 33,3
union all select 44,41,42,43,44
go

--查询
select b.*
from 表A a join 表B b on a.id=b.id
where a.p1<>b.p1
or a.p2<>b.p2
or a.p3<>b.p3
or a.p4<>b.p4
go

--删除测试
drop table 表a,表b

/*--测试结果

ID p1 p2 p3 p4
----------- ----------- ----------- ----------- -----------
33 3 3 33 3

(所影响的行数为 1 行)
--*/
47522341 2004-06-21
  • 打赏
  • 举报
回复
晕掉;时间显示相同的;为什么五星的要比我的快;^_^
chludlf 2004-06-21
  • 打赏
  • 举报
回复
select * from b where ltrim(rtrim(convert(char(10),ID )))+ltrim(rtrim(convert(char(10),p1 )))+ltrim(rtrim(convert(char(10),p2 )))+ltrim(rtrim(convert(char(10),p3 )))+ltrim(rtrim(convert(char(10),p4 ))) not in
(select ltrim(rtrim(convert(char(10),ID )))+ltrim(rtrim(convert(char(10),p1 )))+ltrim(rtrim(convert(char(10),p2 )))+ltrim(rtrim(convert(char(10),p3 )))+ltrim(rtrim(convert(char(10),p4 ))) from a )
------------------------
47522341 2004-06-21
  • 打赏
  • 举报
回复
select *
from B left join A on B.ID = A.ID
where (B.P1 <> A.P1) or
(B.P2 <> A.P2) or
(B.P3 <> A.P3) or
(B.P4 <> A.P4)
zjcxc 2004-06-21
  • 打赏
  • 举报
回复
select b.*
from 表A a join 表B b on a.id=b.id
where a.p1<>b.p1
or a.p2<>b.p2
or a.p3<>b.p3
or a.p4<>b.p4
HUANG_JH 2004-06-21
  • 打赏
  • 举报
回复
好的,我试试看
zjcxc 2004-06-21
  • 打赏
  • 举报
回复
哈,两个库的同意楼上.

22,209

社区成员

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

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