SQL两个表数据对比
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL两个表数据对比相关的知识,希望对你有一定的参考价值。
有表A和表B分别把相同的和不同的数据对比表示出来,要SQL语句
参考技术A select a.*,'相同数据' as note from 表A a left join 表B bon a.name=b.name
where a.age=b.age
union all
select *,'不相同数据' as note from 表A c where c.name not in
(
select a.name from 表A a left join 表B b
on a.name=b.name
where a.age=b.age
) or c.age not in
(
select a.age from 表A a left join 表B b
on a.name=b.name
where a.age=b.age
)
union all
select *,'不相同数据' as note from 表B c where c.name not in
(
select a.name from 表A a left join 表B b
on a.name=b.name
where a.age=b.age
) or c.age not in
(
select a.age from 表A a left join 表B b
on a.name=b.name
where a.age=b.age
)本回答被提问者和网友采纳
如何用SQL对两个文件做比较
参考技术A 比较两个数据库,可以用工具,比如toad等,也可以自己写存储过程来实现,偶介绍一个用sql来核对表结构是否一致,以此类推,大家可以写出对比索引是否一致,对比约束是否一致的sql,该sql的缺点就是只能查出差异,却不知是那个表引起的:select
case
when
a.cnt
=
b.cnt
then
'两个库结构一致'
when
a.cnt
<>
b.cnt
then
'两个库结构不一致'
end
from
(select
count(*)
as
cnt
from
dba_tab_columns
t1,
dba_tab_columns@lnk_db2
t2
where
t1.owner
=
'TAOBAO'
and
t1.owner
=
t2.owner
and
t1.table_name
=
t2.table_name
and
t1.column_name
=
t2.column_name
and
t1.data_type
=
t2.data_type
and
t1.data_length
=
t2.data_length
and
t1.nullable
=
t2.nullable
and
nvl(t1.data_precision,
0)
=
nvl(t2.data_precision,
0)
and
nvl(t1.data_scale,
0)
=
nvl(t2.data_scale,
0))
a,
(select
count(*)
as
cnt
from
dba_tab_columns
where
owner
=
'TAOBAO')
b
以上是关于SQL两个表数据对比的主要内容,如果未能解决你的问题,请参考以下文章