首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql,两个表中的数据对比,该怎么解决

2012-04-02 
sql,两个表中的数据对比查询两个表,比较他们没列的值是否相同,如果不相同,将不相同的插入到另以个临时表中

sql,两个表中的数据对比
查询两个表,比较他们没列的值是否相同,如果不相同,将不相同的插入到另以个临时表中.

因为我要比较很多表,所以希望可以传表名.

这个要用sql写出来。

[解决办法]

SQL code
insert into temp select * from a except select * from binsert into temp select * from b except select * from a
[解决办法]
SQL code
id name age 2 null 18
[解决办法]
探讨

引用:

SQL code
insert into temp select * from a except select * from b
insert into temp select * from b except select * from a
这样会取出不同的吗?

[解决办法]
SQL code
--A,B 两个表--A:--id name age  --1 张三 22--2 张四 21--B:--id name age  --1 张三 22--2 张四 18--那么得到的结果应该是--在一个临时表中插入:--id name age  --2 null 18if OBJECT_ID('temp')is not null drop table tempgo create table temp ( id int ,name varchar(50), age int )if OBJECT_ID('A')is not null drop table Ago create table A ( id int ,name varchar(50), age int )insert into A select 1 ,'张三', 22 union all select 2, '张四' ,21   if OBJECT_ID('B')is not null drop table Bgo create table B ( id int ,name varchar(50), age int )insert into B select 1 ,'张三', 22 union all select 2, '张四' ,18  insert into  temp select * from A except select * from Bselect * from tempid          name                                               age----------- -------------------------------------------------- -----------2           张四                                                 21(1 行受影响)
[解决办法]
去重
SQL code
insert into tempselect * from a except select * from bunionselect * from b except select * from b
[解决办法]
SQL code
--又分的幺妹--方法1select * into #t from a where 1<>1insert into #t select * from a except select * from binsert into #t select * from b except select * from a--方法2select * into #t from a where 1<>1SELECT * FROM (SELECT * FROM aaa UNION SELECT * FROM dbo.sc )AS a EXCEPT(select * from aaa INTERSECT SELECT * FROM dbo.sc)--方法还有很多种... 集合问题
[解决办法]
SQL code
--A,B 两个表--A:--id name age  --1 张三 22--2 张四 21--B:--id name age  --1 张三 22--2 张四 18--那么得到的结果应该是--在一个临时表中插入:--id name age  --2 null 18if OBJECT_ID('temp')is not null drop table tempgo create table temp ( id int ,name varchar(50), age int )if OBJECT_ID('A')is not null drop table Ago create table A ( id int ,name varchar(50), age int )insert into A select 1 ,'张三', 22 union all select 2, '张四' ,21  if OBJECT_ID('B')is not null drop table Bgo create table B ( id int ,name varchar(50), age int )insert into B select 1 ,'张三', 22 union all select 2, '张四' ,18  insert into  temp   select  id ,case when  name IN(select name from B ) then null else name end as 'name' ,age from  (select * from A except select * from B )s select * from temp id          name                                               age----------- -------------------------------------------------- -----------2           NULL                                               21(1 行受影响)
[解决办法]
SQL code
--A,B 两个表--A:--id name age  --1 张三 22--2 张四 21--B:--id name age  --1 张三 22--2 张四 18--那么得到的结果应该是--在一个临时表中插入:--id name age  --2 null 18if OBJECT_ID('temp')is not null drop table tempgo create table temp ( id int ,name varchar(50), age int )if OBJECT_ID('A')is not null drop table Ago create table A ( id int ,name varchar(50), age int )insert into A select 1 ,'张三', 22 union all select 2, '张四' ,21  if OBJECT_ID('B')is not null drop table Bgo create table B ( id int ,name varchar(50), age int )insert into B select 1 ,'张三', 22 union all select 2, '张四' ,18  insert into  temp   select  id ,case when  name IN(select name from B ) then null else name end as 'name' ,age from  (select * from A except select * from B )s select * from temp id          name                                               age----------- -------------------------------------------------- -----------2           NULL                                               21(1 行受影响) 


[解决办法]
小美女,你想太复杂了,直接select * from a except select * from b,没必要取到列,只要取到行还找不到数据吗?

热点排行