lhj2005 发表于 2007-11-14 15:32

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

<P>求助:<BR>两个表a和b,两表中的数据几乎相同,b表比a表多几十条数据,求找出多出的数据的语句<BR>两表中的字段是 ID ,NAME,SEX<BR>select * from b<BR>where b.ID not in (select ID from a)<BR><BR>请求其他语句!(注:b表是临时表,是通过其他查询的临时表)<BR><BR></P>

purana 发表于 2007-11-14 19:36

<P>create table #t1(<BR>id int,<BR>col1 int,<BR>col2 int<BR>)</P>
<P>insert into #t1 values(1,27,3)<BR>insert into #t1 values(2,22,3)<BR>insert into #t1 values(3,22,3)<BR>insert into #t1 values(4,23,3)<BR>insert into #t1 values(5,3,3)</P>
<P><BR>create table #t2(<BR>id int,<BR>col1 int,<BR>col2 int<BR>)</P>
<P>insert into #t2 values(1,27,3)<BR>insert into #t2 values(2,22,3)<BR>insert into #t2 values(3,22,3)<BR>insert into #t2 values(4,23,3)<BR>insert into #t2 values(5,3,3)</P>
<P>--这两行是多出来的.<BR>insert into #t2 values(7,23,3)<BR>insert into #t2 values(8,3,3)</P>
<P>select b.* from #t1 a full join #t2 b on a.id=b.id where a.id is null</P>
<P>drop table #t1,#t2<BR><BR><BR>/*<BR>id          col1        col2        <BR>----------- ----------- ----------- <BR>7           23          3<BR>8           3           3<BR>*/</P>

XieLi 发表于 2007-11-15 08:45

[em17]

cyyu_ryh 发表于 2007-11-15 12:48

看不太懂<br>full 是什么?<br>

purana 发表于 2007-11-15 12:54

<P>full         <BR>n.<BR>全部, 完整</P>
<P>adj.<BR>充满的, 完全的, 丰富的, 完美的, 详尽的, 丰满的</P>
<P>adv.<BR>完全地, 整整, 十分<BR></P>

lhj2005 发表于 2007-11-20 13:09

<P>select b.* from #t1 a full join #t2 b on a.id=b.id where a.id is null</P>
<P>drop table #t1,#t2<BR><BR>为什么最后要删除两个表啊!!![em09]</P>

bygg 发表于 2007-11-20 13:10

都是临时的,不删掉,留着做什么呢??占空间

lhj2005 发表于 2007-11-20 13:15

我原意是这样的:<BR> 预置条件  a表,c表,d表,e表<BR>              <BR>     a表是个存在的表格,而b表是通过SQL语句查询a,c,d表得到的结果作为的临时表格,并不是建立临时表<BR>                    (select * from c,d,e) as b <BR>      

缘吇弹 发表于 2007-11-20 16:53

<DIV class=quote><B>以下是引用<U>purana</U>在2007-11-15 12:54:05的发言:</B><BR>
<P>full         <BR>n.<BR>全部, 完整</P>
<P>adj.<BR>充满的, 完全的, 丰富的, 完美的, 详尽的, 丰满的</P>
<P>adv.<BR>完全地, 整整, 十分<BR></P></DIV>
<P>哈哈...竟教起英国历史来了.</P>

purana 发表于 2007-11-20 17:50

to 8楼..<BR>..不明白.

卡卡艾 发表于 2007-11-20 20:18

full连接.<BR>以后你还会用到left join,right join,cross join 等等.[em02]

缘吇弹 发表于 2007-11-20 22:34

<DIV class=quote><B>以下是引用<U>lhj2005</U>在2007-11-20 13:15:27的发言:</B><BR>我原意是这样的:<BR>预置条件  a表,c表,d表,e表<BR>              <BR>     a表是个存在的表格,而b表是通过SQL语句查询a,c,d表得到的结果作为的临时表格,并不是建立临时表<BR>                    (select * from c,d,e) as b <BR>      </DIV>
<P>你开始又不说清楚,人家purana版主又怎么知道.<BR><BR>purana上边的方法已经实现了你预期的效果,LZ不要钻牛角尖.<BR><BR>实现你所说的方法也不难嘛:<BR><BR>create database abcde<BR>go<BR>use abcde<BR>go<BR>create table a(<BR>id int primary key,<BR>name char(8),<BR>sex char(2)<BR>)<BR>go<BR>create table c(<BR>id int primary key,<BR>name char(8),<BR>sex char(2)<BR>)<BR>go<BR>create table d(<BR>id int primary key,<BR>name char(8),<BR>sex char(2)<BR>)<BR>go<BR>create table e(<BR>id int primary key,<BR>name char(8),<BR>sex char(2)<BR>)<BR>go
<P>insert into a<BR>values(101,'张三','男')<BR>go<BR>insert into a<BR>values(102,'李四','女')<BR>go<BR>insert into a<BR>values(103,'王五','男')<BR>go
<P>insert into c<BR>values(101,'张三','男')<BR>go<BR>insert into c<BR>values(102,'李四','女')<BR>go<BR>insert into c<BR>values(103,'王五','男')<BR>go<BR>insert into c<BR>values(104,'陈六','女') --多出的<BR>go
<P>insert into d<BR>values(101,'张三','男')<BR>go<BR>insert into d<BR>values(102,'李四','女')<BR>go<BR>insert into d<BR>values(103,'王五','男')<BR>go<BR>insert into d<BR>values(104,'陈六','女') --多出的<BR>go
<P>insert into e<BR>values(101,'张三','男')<BR>go<BR>insert into e<BR>values(102,'李四','女')<BR>go<BR>insert into e<BR>values(103,'王五','男')<BR>go<BR>insert into e<BR>values(104,'陈六','女') --多出的<BR>go
<P>use abcde<BR>go<BR>select b.* from <BR>(select c.* from c,d,e <BR>where (c.id=d.id) and (c.id=e.id) and (d.id=e.id)<BR>) as b<BR>where b.id not in(select id from a)<BR>go<BR><BR>查询结果:<BR>id   name   sex<BR>---  ----   ----<BR>104  陈六   女</P>

zero_first 发表于 2008-4-9 11:26

可以用left outer join 和right outer join

页: [1]

编程论坛