请教:两张表一张表中的记录是另一表的子集,怎么查询出该表中非另一表子集的记录

Dream_1986 2009-10-26 02:43:21
现在有两张表,一张表有2826条记录称为表a,另外一张表,有7000多条记录称为表b,现在表a中的记录都是表b中的,现在要求将表b中不包含a中的记录查询出来,应该怎么做????谢谢!!!
...全文
105 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
忆轩辕 2009-10-26
  • 打赏
  • 举报
回复
SQL2000只能用not exists判断字段(如果有一唯一字段的话,没有就全字段判断吧)
SQL2005用except
SIMENYU 2009-10-26
  • 打赏
  • 举报
回复

--SQL 2000
select * from b where id not in (select id from a)
--or
select * from b where not exists (select 1 from a where a.id=b.id) --id是唯一列;

--SQL 2005,若表结构相同,则:
select * from b
except
select * from a

Dream_1986 2009-10-26
  • 打赏
  • 举报
回复
其实,a表中还有部分b表中不能完全匹配的记录。。
navy887 2009-10-26
  • 打赏
  • 举报
回复
Select  *  from  b  Where not exist (select * from a)
--或
Select * from b Where id not in (select id from a)
Flyingdragon168 2009-10-26
  • 打赏
  • 举报
回复
如何判断A表在B表中,应当有个一个或多个关键栏位吧。
SELECT * FROM b WHERE ColumX not IN (SLEECT ColumnX FROM A)
其中ColumnX 表示关键栏位:
如果是一个栏位,则Column1
如果多个,可以IsNull(Colomn_No1,'')+IsNull(Column_No2,'')等。
华夏小卒 2009-10-26
  • 打赏
  • 举报
回复

if object_id('num')is not null drop table num
go
create table num( n int )
insert into num
select 1 union
select 2 union
select 3 union
select 4 union
select 5 union
select 6 union
select 7 union
select 8 union
select 9


if object_id('num1')is not null drop table num1
go
create table num1( n int )
insert into num1
select 1 union
select 2 union
select 7 union
select 8 union
select 9

select * from num
except
select * from num1


n
-----------
3
4
5
6

(4 行受影响)

xuam 2009-10-26
  • 打赏
  • 举报
回复
select * from b where not exists (select * from a)
--小F-- 2009-10-26
  • 打赏
  • 举报
回复
select  *  from  b  where not exists (select  * from  a where id=b.id)
--小F-- 2009-10-26
  • 打赏
  • 举报
回复
select  *  from  b  where not exist (select  * from  a where id=b.id)
SQL77 2009-10-26
  • 打赏
  • 举报
回复
SELECT * FROM B WHERE ID NOT IN (SELECT ID FROM A )
ID唯一
SQL77 2009-10-26
  • 打赏
  • 举报
回复
SELECT * FROM B WHERE NOT EIXSTS(SELECT 1 FROM A WHERE 关联字段)
2005用EXCEPT
xuam 2009-10-26
  • 打赏
  • 举报
回复
select * from b where not exist (select * from a)

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧