注册 登录
编程论坛 SQL Server论坛

[求助]如何核对两表中的数据

lhj2005 发布于 2007-11-14 15:32, 2937 次点击

求助:
两个表a和b,两表中的数据几乎相同,b表比a表多几十条数据,求找出多出的数据的语句
两表中的字段是 ID ,NAME,SEX
select * from b
where b.ID not in (select ID from a)

请求其他语句!(注:b表是临时表,是通过其他查询的临时表)

12 回复
#2
purana2007-11-14 19:36

create table #t1(
id int,
col1 int,
col2 int
)

insert into #t1 values(1,27,3)
insert into #t1 values(2,22,3)
insert into #t1 values(3,22,3)
insert into #t1 values(4,23,3)
insert into #t1 values(5,3,3)


create table #t2(
id int,
col1 int,
col2 int
)

insert into #t2 values(1,27,3)
insert into #t2 values(2,22,3)
insert into #t2 values(3,22,3)
insert into #t2 values(4,23,3)
insert into #t2 values(5,3,3)

--这两行是多出来的.
insert into #t2 values(7,23,3)
insert into #t2 values(8,3,3)

select b.* from #t1 a full join #t2 b on a.id=b.id where a.id is null

drop table #t1,#t2


/*
id col1 col2
----------- ----------- -----------
7 23 3
8 3 3
*/

#3
XieLi2007-11-15 08:45
#4
cyyu_ryh2007-11-15 12:48
看不太懂
full 是什么?
#5
purana2007-11-15 12:54

full
n.
全部, 完整

adj.
充满的, 完全的, 丰富的, 完美的, 详尽的, 丰满的

adv.
完全地, 整整, 十分

#6
lhj20052007-11-20 13:09

select b.* from #t1 a full join #t2 b on a.id=b.id where a.id is null

drop table #t1,#t2

为什么最后要删除两个表啊!!!

#7
bygg2007-11-20 13:10
都是临时的,不删掉,留着做什么呢??占空间
#8
lhj20052007-11-20 13:15
我原意是这样的:
预置条件 a表,c表,d表,e表

a表是个存在的表格,而b表是通过SQL语句查询a,c,d表得到的结果作为的临时表格,并不是建立临时表
(select * from c,d,e) as b
#9
缘吇弹2007-11-20 16:53
以下是引用purana在2007-11-15 12:54:05的发言:

full
n.
全部, 完整

adj.
充满的, 完全的, 丰富的, 完美的, 详尽的, 丰满的

adv.
完全地, 整整, 十分

哈哈...竟教起英国历史来了.

#10
purana2007-11-20 17:50
to 8楼..
..不明白.
#11
卡卡艾2007-11-20 20:18
full连接.
以后你还会用到left join,right join,cross join 等等.
#12
缘吇弹2007-11-20 22:34
以下是引用lhj2005在2007-11-20 13:15:27的发言:
我原意是这样的:
预置条件 a表,c表,d表,e表

a表是个存在的表格,而b表是通过SQL语句查询a,c,d表得到的结果作为的临时表格,并不是建立临时表
(select * from c,d,e) as b

你开始又不说清楚,人家purana版主又怎么知道.

purana上边的方法已经实现了你预期的效果,LZ不要钻牛角尖.

实现你所说的方法也不难嘛:

create database abcde
go
use abcde
go
create table a(
id int primary key,
name char(8),
sex char(2)
)
go
create table c(
id int primary key,
name char(8),
sex char(2)
)
go
create table d(
id int primary key,
name char(8),
sex char(2)
)
go
create table e(
id int primary key,
name char(8),
sex char(2)
)
go

insert into a
values(101,'张三','男')
go
insert into a
values(102,'李四','女')
go
insert into a
values(103,'王五','男')
go

insert into c
values(101,'张三','男')
go
insert into c
values(102,'李四','女')
go
insert into c
values(103,'王五','男')
go
insert into c
values(104,'陈六','女') --多出的
go

insert into d
values(101,'张三','男')
go
insert into d
values(102,'李四','女')
go
insert into d
values(103,'王五','男')
go
insert into d
values(104,'陈六','女') --多出的
go

insert into e
values(101,'张三','男')
go
insert into e
values(102,'李四','女')
go
insert into e
values(103,'王五','男')
go
insert into e
values(104,'陈六','女') --多出的
go

use abcde
go
select b.* from
(select c.* from c,d,e
where (c.id=d.id) and (c.id=e.id) and (d.id=e.id)
) as b
where b.id not in(select id from a)
go

查询结果:
id name sex
--- ---- ----
104 陈六 女

#13
zero_first2008-04-09 11:26
可以用left outer join 和right outer join
1