sql 2000如何获取2表的差集?

stg609 2009-03-19 11:39:25
现有如下两张表(共3列,3列合为一个主键):

表1
--------------
列1 列2 列3
--------------
1 a1 b1
1 a2 b2
2 a6 b6

表2
--------------
列1 列2 列3
--------------
1 a1 b2
1 a2 b2
3 a6 b6
现在我希望得到表1中有的但是表2中没有的记录,该怎么写?
得出来的结果应该是
--------------
列1 列2 列3
--------------
1 a1 b1
2 a6 b6

在集合运算看来,这个应该算是差集吧,但是怎么在sql 2000中实现呢?
...全文
286 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
htl258_Tony 2009-03-19
  • 打赏
  • 举报
回复
7楼改下,是差集.
百年树人 2009-03-19
  • 打赏
  • 举报
回复
2005还可以用EXCEPT

select * from [表1]
EXCEPT
select * from [表2]

/**
列1 列2 列3
----------- ---- ----
1 a1 b1
2 a6 b6

(所影响的行数为 2 行)
**/
htl258_Tony 2009-03-19
  • 打赏
  • 举报
回复
--应该是交集吧,2005
select * from tb1
except
select * from tb2
牙签是竹子的 2009-03-19
  • 打赏
  • 举报
回复
dawugui 2009-03-19
  • 打赏
  • 举报
回复
create table tb1(col1 int,col2 nvarchar(10),col3 nvarchar(10))
create table tb2(col1 int,col2 nvarchar(10),col3 nvarchar(10))
insert into tb1 select 1,'a1','b1'
union all select 1,'a2','b2'
union all select 2,'a6','b6'
insert into tb2 select 1,'a1','b2'
union all select 1,'a2','b2'
union all select 3,'a6','b6'

select t.* from tb1 t where not exists (select 1 from tb2 where col1 = t.col1 and col2 = t.col2 and col3 = t.col3)

drop table tb1 , tb2

/*
col1 col2 col3
----------- ---------- ----------
1 a1 b1
2 a6 b6

(所影响的行数为 2 行)
*/
等不到来世 2009-03-19
  • 打赏
  • 举报
回复
if object_id('[表1]') is not null drop table [表1]
go
create table [表1]([列1] int,[列2] varchar(2),[列3] varchar(2))
insert [表1]
select 1,'a1','b1' union all
select 1,'a2','b2' union all
select 2,'a6','b6'
if object_id('[表2]') is not null drop table [表2]
go
create table [表2]([列1] int,[列2] varchar(2),[列3] varchar(2))
insert [表2]
select 1,'a1','b2' union all
select 1,'a2','b2' union all
select 3,'a6','b6'

--select * from [表1]
--select * from [表2]

select * from [表1]
where checksum(*) not in (select checksum(*) from [表2])
--测试结果:
/*
列1 列2 列3
----------- ---- ----
1 a1 b1
2 a6 b6

(2 行受影响)
*/
dawugui 2009-03-19
  • 打赏
  • 举报
回复
select m.* from tb1 m where not exists (select 1 from tb1 where col1 = t.col1 and col2 = t.col2 and col3 = t.col3)
ChinaJiaBing 2009-03-19
  • 打赏
  • 举报
回复

declare @a table (列1 int,列2 nvarchar(10),列3 nvarchar(10))
declare @b table (列1 int,列2 nvarchar(10),列3 nvarchar(10))
insert into @a select 1,'a1','b1'
union all select 1,'a2','b2'
union all select 2,'a6','b6'
insert into @b select 1,'a1','b2'
union all select 1,'a2','b2'
union all select 3,'a6','b6'
select * from @a a where not exists (select 1 from @b b where a.列1=b.列1 and a.列2=b.列2 and a.列3=b.列3)

列1 列2 列3
----------- ---------- ----------
1 a1 b1
2 a6 b6

(2 行受影响)

天-笑 2009-03-19
  • 打赏
  • 举报
回复


select * from t1 where convert(varchar(10),列1)+'-'+convert(varchar(10),列2)+'-'+convert(varchar(10),列3) not in (select convert(varchar(10),列1)+'-'+convert(varchar(10),列2)+'-'+convert(varchar(10),列3) from t2)


stg609 2009-03-19
  • 打赏
  • 举报
回复
谢谢各位~~总结下,大家的方法主要有:
1.not exists 来排除相同的
2.对每条记录取一个值来唯一表示该记录,然后通过比较该值来判断是是否相同并排除。这个又有2种办法即1楼的和4楼的
1楼将每条记录中的所有列值取出后合为一个值进行比较(该方法,本人未测试)
4楼则是利用checksum来取每条记录相应列的哈希值,代码看上去比较neat

------------------------
另外,也有朋友提到2005下的except方法,我也很想用这个方法,看上去代码更少更整洁,但是谁让sql 2000 不支持呢。

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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