sql分页如何用exists替换not in???

askquestion_004 2012-04-30 04:15:02
SELECT TOP 5 *
FROM table
WHERE (id NOT IN
(SELECT TOP 10 id
FROM table))

上面是段分页用的sql,如何用exists替换not in
...全文
212 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yukai08621 2012-04-30
  • 打赏
  • 举报
回复
select top * from table where id not in (select id from table )
  • 打赏
  • 举报
回复
就你这两个语句差不多差不多。
askquestion_004 2012-04-30
  • 打赏
  • 举报
回复
SELECT TOP 5 *
FROM table
WHERE (id NOT IN
(SELECT TOP 10 id
FROM table))

SELECT TOP 5 *
FROM test b
WHERE not exists(select 1 from(select top 15 id from test)a where b.id<=a.id)

哪位能分析下效率不???
IEEE_China 2012-04-30
  • 打赏
  • 举报
回复
回复 T,
哦,没注意,看错了。呵呵
没测试,上来就直接提交代码了
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

你这SQL语句什么也查不到

SQL code


--

SELECT TOP 5 *
FROM table
WHERE NOT exists
(SELECT TOP 10 id
FROM table)
[/Quote]

go
if OBJECT_ID('test')is not null
drop table test
go
create table test(
id int identity(1,1),
value int
)
go
declare @b int
set @b=1
while @b<100
begin
insert test
select @b
set @b=@b+1
end


SELECT TOP 5 *
FROM test
WHERE (id NOT IN
(SELECT TOP 10 id
FROM test))
/*
id value
16 16
17 17
18 18
19 19
20 20
*/

--他的语句能实现,你是不是没测试数据啊
  • 打赏
  • 举报
回复

go
if OBJECT_ID('test')is not null
drop table test
go
create table test(
id int identity(1,1),
value int
)
go
declare @b int
set @b=1
while @b<100
begin
insert test
select @b
set @b=@b+1
end


SELECT TOP 5 *
FROM test
WHERE (id NOT IN
(SELECT TOP 15 id
FROM test))
/*
id value
16 16
17 17
18 18
19 19
20 20
*/

SELECT TOP 5 *
FROM test b
WHERE not exists(select 1 from(select top 15 id from test)a where b.id<=a.id)
/*
id value
16 16
17 17
18 18
19 19
20 20
*/

IEEE_China 2012-04-30
  • 打赏
  • 举报
回复
你这SQL语句什么也查不到



--

SELECT TOP 5 *
FROM table
WHERE NOT exists
(SELECT TOP 10 id
FROM table)

34,576

社区成员

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

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