日期:2014-05-18  浏览次数:20404 次

两个表关联查询
有两个表   A   B
表A
字段A1   字段A2   字段A3   字段A4
001           12           32           14
002           11           22           33

表B
字段B1   字段B2   字段B3   字段B4
001           12           32           14
003           10           12           10

表A与表B的关联字段为   '字段A1   与   字段B1 '

我想查询的结果是:表A中有的记录而表B中没有   用   left   join   ......on   如何实现?
谢谢!(如果有其他好的方法也行   但not   in语句除外)

------解决方案--------------------
try


Select
A.*
From
A
Left Join
B
On A.A1 = B.B1
Where B.B1 Is Null
------解决方案--------------------
select * from 表a a left join 表b b
on a.字段a1=b.字段b1
------解决方案--------------------
如果字段都是char或varchar

select * from 表A where 字段A1+ 字段A2+ 字段A3+ 字段A4 not in
(select 字段A1+ 字段A2+ 字段A3+ 字段A4 from b)
------解决方案--------------------
或者用checksum
------解决方案--------------------
lz说了不用not in
------解决方案--------------------
那種寫法不好
------解决方案--------------------
這麼寫吧

create table #t1(A1 varchar(10),A2 varchar(10),A3 varchar(10))
insert #t1
select '001 ', '10 ', '13 '
union all
select '002 ', '11 ', '13 '
union all
select '003 ', '10 ', '14 '
union all
select '005 ', '11 ', '55 '
go
create table #tt1(B1 varchar(10),B2 varchar(10),B3 varchar(10))
insert #tt1
select '001 ', '10 ', '13 '
union all
select '004 ', '11 ', '13 '
union all
select '003 ', '10 ', '14 '
union all
select '005 ', '02 ', '01 '

select #t1.* from #t1 left join #tt1 on #t1.A1=#tt1.B1 and A1 = B1 And A2 =B2 And A3=B3 where #tt1.B1 is null
drop table #t1
drop table #tt1
------解决方案--------------------
select *
from 表A
where not exists(select *
from 表B
where 表B.字段B1 = 表A.字段A1)