求一条SQL语句,要求效率稍微高一点的,

周睿 2010-05-04 10:57:55
要求我输入产品ID和一个数字,
比如产品ID为5,数字为3

我想查出5后面第3位的产品ID,
和5前面第3位的产品ID,
不要存储过程,SQL语句就可以了,
...全文
148 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzh_911 2010-05-04
  • 打赏
  • 举报
回复
xuexi....
feixianxxx 2010-05-04
  • 打赏
  • 举报
回复
2005利用row_number 生成的CTE 进行筛选是效率最高的
2000导入临时表
喜-喜 2010-05-04
  • 打赏
  • 举报
回复
'这样看起来舒服些...'
--------------------SQL Server数据格式化工具-------------------
---------------------------------------------------------------
-- DESIGNER :happycell188(喜喜)
-- QQ :584738179
-- Development Tool :Microsoft Visual C++ 6.0 C Language
-- FUNCTION :CONVERT DATA TO T-SQL
---------------------------------------------------------------
-- Microsoft SQL Server 2005
-- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
---------------------------------------------------------------
---------------------------------------------------------------

use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
ID char(3),
jno char(3)
)
go
--插入测试数据
insert into tb select '1','j1'
union all select '4','j3'
union all select '3','j4'
union all select '7','j3'
union all select '5','j1'
union all select '9','j2'
union all select '8','j1'
go
--代码实现
if object_id('test.dbo.proc_test') is not null drop proc proc_test
go
create proc proc_test
@idinput varchar(10),
@i int
as
begin
declare @idd varchar(10)
if object_id('test.dbo.#t') is not null drop table #t
select idd=row_number()over(order by getdate()),* into #t from tb
select @idd=idd from #t where ID=@idinput
select ID from #t where idd=@idd+3 or idd=@idd-3
end
go

--> 测试存储过程
exec proc_test 5,3

/*
ID
-------
4
*/

exec proc_test 7,3

/*
ID
-------
1
8
*/
喜-喜 2010-05-04
  • 打赏
  • 举报
回复
--------------------SQL Server数据格式化工具-------------------
---------------------------------------------------------------
-- DESIGNER :happycell188(喜喜)
-- QQ :584738179
-- Development Tool :Microsoft Visual C++ 6.0 C Language
-- FUNCTION :CONVERT DATA TO T-SQL
---------------------------------------------------------------
-- Microsoft SQL Server 2005
-- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
---------------------------------------------------------------
---------------------------------------------------------------

use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
ID char(3),
jno char(3)
)
go
--插入测试数据
insert into tb select '1','j1'
union all select '4','j3'
union all select '3','j4'
union all select '7','j3'
union all select '5','j1'
union all select '9','j2'
union all select '8','j1'
go
--代码实现
if object_id('test.dbo.proc_test') is not null drop proc proc_test
go
create proc proc_test
@idinput varchar(10),
@i int
as
begin
declare @idd varchar(10)
if object_id('test.dbo.#t') is not null drop table #t
select idd=row_number()over(order by getdate()),* into #t from tb
select @idd=idd from #t where ID=@idinput
select ID from #t where idd=@idd+3 or idd=@idd-3
end
go

--> 测试存储过程
exec proc_test 5,3

/*
ID
-------
4
*/

exec proc_test 7,3

/*
ID
-------
1
8
*/
aaron_gl 2010-05-04
  • 打赏
  • 举报
回复
declare @num int
select * from a where sno between @num-3 and SNO=@num+3
周睿 2010-05-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fcpslove 的回复:]
declare @num int
select * from a where sno=@num-3 or SNO=@num+3
[/Quote]
不对,ID不是连续的,但无重复,
周睿 2010-05-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 rmljoe 的回复:]
ID是顺序无重复无断序数字吗
[/Quote]
ID不是顺序的,但是无重复
rmljoe 2010-05-04
  • 打赏
  • 举报
回复
ID是顺序无重复无断序数字吗
fcpslove 2010-05-04
  • 打赏
  • 举报
回复
declare @num int
select * from a where sno=@num-3 or SNO=@num+3
周睿 2010-05-04
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 yangjinck 的回复:]
declare @num=5 int
select * from table where id=@num+3
select * from table where id=@num-3

就这样
[/Quote]
ID不连续,这样没用
yangjinCK 2010-05-04
  • 打赏
  • 举报
回复
declare @num=5 int
select * from table where id=@num+3
select * from table where id=@num-3

就这样
happy664618843 2010-05-04
  • 打赏
  • 举报
回复
周睿 2010-05-04
  • 打赏
  • 举报
回复
不用2005,要能同时兼容2000和ACC的代码
周睿 2010-05-04
  • 打赏
  • 举报
回复
用2000和ACC

34,576

社区成员

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

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