SQL语句中except是怎样用的?

treehome 2002-10-24 05:24:57
SQL语句中except是怎样用的?
我的写法是:
select * from a
except
select * from b
目的是找出表a不在表b中的记录(a,b表的结构是一样的)
有无详细的说明用法?
...全文
2993 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
j9988 2002-10-25
  • 打赏
  • 举报
回复
多谢了,不理它,反正知道两句一样就行。我原以为EXISTS快
j9988 2002-10-25
  • 打赏
  • 举报
回复
我测了,索引后还慢1倍
但CPU使用100%,可能跟机器性能有关
昵称被占用了 2002-10-25
  • 打赏
  • 举报
回复
语句也拷贝错了:

SELECT CONVERT(CHAR(30),GETDATE(),121)
select * from t where id not in (select id from r)
SELECT CONVERT(CHAR(30),GETDATE(),121)
select * from t a where not exists(select id from r where id=a.id)
SELECT CONVERT(CHAR(30),GETDATE(),121)
昵称被占用了 2002-10-25
  • 打赏
  • 举报
回复
错了,第一句是:
create table t(id int identity,n int)
go
昵称被占用了 2002-10-25
  • 打赏
  • 举报
回复
测试方法:
create table t(id int identity(5,1),n int)
go
create table r(id int identity(5,1),n int)
go

insert t select 1

declare @i int
set @i=0
while @i<=20
begin
insert t select N from t
set @i=@i+1
end
go

insert r(n)
select n from t

go

SELECT CONVERT(CHAR(30),GETDATE(),121)

select * from t where id not in (select id+1 from t)
SELECT CONVERT(CHAR(30),GETDATE(),121)
select * from t a where not exists(select id from t where id+1=a.id)
SELECT CONVERT(CHAR(30),GETDATE(),121)
go


create clustered index id1 on t(id)
go
create clustered index id2 on r(id)
go
SELECT CONVERT(CHAR(30),GETDATE(),121)

select * from t where id not in (select id+1 from t)
SELECT CONVERT(CHAR(30),GETDATE(),121)
select * from t a where not exists(select id from t where id+1=a.id)
SELECT CONVERT(CHAR(30),GETDATE(),121)
go


结果:
有索引比没有索引快,但很不明显。
not in和not exists 完全一样,(应该是自动优化的)


j9988 2002-10-25
  • 打赏
  • 举报
回复
去年的价,所以现在后悔得很
supsuccess 2002-10-25
  • 打赏
  • 举报
回复
好像不是内存的原因吧?
我这里加不加clustered index 一个样,点F5都是直接出结果

什么地方? 69!这么便宜!!
j9988 2002-10-25
  • 打赏
  • 举报
回复
to: gejanry(gejanry)
supsuccess(火气不小) 不是回答了?
j9988 2002-10-25
  • 打赏
  • 举报
回复
要加内存了,我去年69元买了六条送人。现在好心痛。
gejanry 2002-10-25
  • 打赏
  • 举报
回复
你们的话题扯远了。还是回答人家的问题吧。
except应该怎么用呢?
j9988 2002-10-25
  • 打赏
  • 举报
回复
你的内存比我大。我才128M,内存
supsuccess 2002-10-25
  • 打赏
  • 举报
回复
总共有数据209万多
奇怪,我这里执行时,4个查询语句几乎都是0秒
WHY ?
win2kserver(没有SP)+sqlserver2k8.00.194+ 256M + p3 1.0G
昵称被占用了 2002-10-25
  • 打赏
  • 举报
回复
测试还没有结束,估计试你的语句和supsuccess(火气不小) 的语句不一样,主要试你的id+1==A.ID中的id+1是表达式,用不到索引,而supsuccess(火气不小)的a.keyname=b.keyname两边都能用到索引。

昵称被占用了 2002-10-25
  • 打赏
  • 举报
回复
我试一下先!
j9988 2002-10-24
  • 打赏
  • 举报
回复
create table t(id int identity,n int)
go
insert ta select 1
declare @i int
set @i=0
while @i<=20
begin
insert ta select a from ta
set @i=@i+1
end
select @@identity---208万
select * from t where id not in (select id+1 from t)
select * from t a where not exists(select id from t where id+1=a.id)
go
--都是35秒
create clustered index id1 on t(id)
go
select * from t where id not in (select id+1 from t)
select * from t a where not exists(select id from t where id+1=a.id)
go
--都是1分9秒
j9988 2002-10-24
  • 打赏
  • 举报
回复
然后对keyname建clustered index速度慢了一倍
j9988 2002-10-24
  • 打赏
  • 举报
回复
奇怪,supsuccess两句的速度竟差不多,那还是用后面一句省力
supsuccess 2002-10-24
  • 打赏
  • 举报
回复
1.
select * from a where not exists(select * from b where a.keyname=b.keyname)
2.
select * from a where keyname not in(select keyname from b)
scottwhb 2002-10-24
  • 打赏
  • 举报
回复
select * from a
not in (select * from b)

34,588

社区成员

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

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