一个有关SQL语句查询,十分紧急,Help?

heyibo78 2001-04-17 06:08:00
我怎样在SQL Server中获得要查询的当前行,如果我要返回以当前行为中心,上下三条的数据,应该怎样写这样的SQL语句?
例如:
表A:
ID Name
1 A2
2 As
3 tt
4 B2
5 Bg
6 DD
7 Df
8 23
9 hh
10 kk

现在我要找以ID=3 并且按照Name排序的上下各三条的数据,怎样写SQL语句,
请大虾们指点。
...全文
217 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwq_jeff 2001-04-24
  • 打赏
  • 举报
回复
假设表名为table1
select * from (select top 3 u1.id,u1.name from table1 u1
where u1.name < (select t1.name from table1 t1 where t1.id = 3 )
order by u1.name desc) v1
union
select * from ( select top 3 u2.id ,u2.name from table1 u2
where u2.name > (select t1.name from table1 t1 where t1.id = 3 )
order by u2.name ) v2
union
select * from table1 where id = 3

yaw 2001-04-24
  • 打赏
  • 举报
回复
需求背景是什么?
heyibo78 2001-04-23
  • 打赏
  • 举报
回复
我想我可能没有把问题说清楚,我想做一个这样的查询:在一个员工表中有字段id,sex,name,tele,num等等,其中,id是可以主键,我想先按name降序排列的记录,并查出id=5为中心上下三条的记录,因为字段name不是唯一索引,所以只能按name降序排列,id=5的当前行号@CurRow,在把(@CurRow-3,@CurRow+3)的记录查出来。wwl007(疑难杂症)的方法可以实现,但是当id不是主键,而是(id,num)为联合主键,查id=5,num=s7时,上述方法就不能满足要求。 
LaoZheng 2001-04-23
  • 打赏
  • 举报
回复
要有主Key才行
seven_wong 2001-04-22
  • 打赏
  • 举报
回复
to yangzi
“ 尽可能不要用游标 ?”是什么意思呢

yaw 2001-04-21
  • 打赏
  • 举报
回复
既然有这样的需求救应该设置流水号了,控制不能删除。
yangzi 2001-04-21
  • 打赏
  • 举报
回复
什么ID不唯一?
你用的是联合主键?
还是压根就没有主键。
那你怎么保证数据的唯一性?

尽可能不要用游标。
cyd6812 2001-04-21
  • 打赏
  • 举报
回复
可以用游标如下实现:具体code 你在研究。
declare @T_corpID integer
declare @T_corpname char(20)

DECLARE T_cursor CURSOR FOR
SELECT corpID,Corpname FROM corp
where corpid > 7-4


OPEN T_cursor

FETCH FROM T_cursor
into @T_corpID,@T_corpname

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.


print convert(char,@T_corpID)+' '+ @T_corpname

fetch next from T_cursor
into @T_corpID,@T_corpname

END

CLOSE T_cursor
DEALLOCATE T_cursor
GO
heyibo78 2001-04-21
  • 打赏
  • 举报
回复
怎样使用游标,yaw,你能说详细点吗?
heyibo78 2001-04-19
  • 打赏
  • 举报
回复
非常感谢wwl007和其他大虾,但是当id不是唯一值的时候,上述方法就不能保证查找出来的值是正确的,用between的朋友可能没有理解我的意思。
yaw 2001-04-19
  • 打赏
  • 举报
回复
Use Cursor。
adidasprince 2001-04-19
  • 打赏
  • 举报
回复
一句sql语句不可能,这7条记录在逻辑上没有共性,系统根本无法知道如何去定位的。
LI_xiufu 2001-04-18
  • 打赏
  • 举报
回复
建议用临时表,其中临时表加一自动加1的int 字段recordID
create table #temptabel (recordID int identity (1, 1),ID int ,name char(10))
insert into #temptable
select a.ID,a.name from yourtable order by name
然后
select ID,name from #temptable a ,(select recordID from #temptable where name=@inputname) b
where a.recordID between b.recordID-3 and b.recordID+3
wwl007 2001-04-17
  • 打赏
  • 举报
回复
//求出编号
select count(*) into @kkk from tablename a,(select name as iname from tablename whereid=@id) b where a.name<=b.iname
//去值
select c.id,c.name from (select (count(*) from tablename a where a.name<b.name) as iid,
b.id,b.name from tablename b where tablename) c where
c.iid>=@kkk-3 and c.iid<@kkk+3

wwl007 2001-04-17
  • 打赏
  • 举报
回复
horby(三级程序员) !
可能吗!你的
between 后面的参数是什么啊!

horby 2001-04-17
  • 打赏
  • 举报
回复
就用between解决不了吗?
wwl007 2001-04-17
  • 打赏
  • 举报
回复
select * from tablename where id=@input
union
select top 1 tablename.id,tablename.name from tablename,(select name from tablename where id=@input) as b where
a.name<b.name
union
select top 1 tablename.id,tablename.name from tablename,(select name from tablename where id=@input) as b where
a.name<b.name order by tablename.name ASC
zzhphp 2001-04-17
  • 打赏
  • 举报
回复
试下这个sql语句:select gc1id,gc1name from t_goods_class1
         where gc1id between 07-3 and 07+3
          order by gc1name
我的表:(以GC1ID=07为中心)
t_goods_class1
-------------------------------------
GC1ID GC1NAME
02 包装印刷
03 保安、救援
04 餐饮住宿旅游
05 仓储
06 传播媒体
07 电信
08 通讯
09 电子
10 儿童用品
12 物业
13 纺织、印染
14 服装、服饰
15 钢材
结果:
---------------------------------------
GC1ID GC1NAME
04 餐饮住宿旅游
05 仓储
06 传播媒体
07 电信
08 通讯
09 电子
10 儿童用品


也訢可以帮你的忙,
(我用的是oracle,我想也许sql server中应有更简单的函数吧)可以给我分吗?:)
lldwolf 2001-04-17
  • 打赏
  • 举报
回复
GZ

34,590

社区成员

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

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