当前位置:编程学习 > C#/ASP.NET >>

求一个sql语句。。。。按排名单位间隔查询。。。

我现在要查询一个表A,假如用户排名rank=50,单位间隔=3 

我想要查询的结果是 在rank之前,也就是排名要在50之前,并且间隔是 3
那么结果是47,44,41,38,35。。。。。

谢谢了。 --------------------编程问答-------------------- 表A中可以用的字短有userID,rank --------------------编程问答-------------------- select rank,* from A where =3 and rank<50 order by rank desc --------------------编程问答-------------------- ……

select * from A where rank<50 and 单位间隔=3 --------------------编程问答-------------------- 不是啊,假如当前用户的排名50,需要查出的排名单位间隔是3 ,那么我就需要把在该用户前面的,并且用户用当前的排名减去单位间隔,就是47,44,41,38,35。。。。。

但是不知道怎么写。
--------------------编程问答-------------------- 求sql达人给个思路。。。。急啊 。。。 --------------------编程问答-------------------- --------------------编程问答--------------------
引用 3 楼  的回复:
……

select * from A where rank<50 and 单位间隔=3  group by rank DESC--(降序) --------------------编程问答--------------------
引用 7 楼  的回复:
引用 3 楼 的回复:

……



select * from A where rank<50 and 单位间隔=3 group by rank DESC--(降序)

单位间隔不是一个字段阿, --------------------编程问答--------------------

declare @i int
set @i = 1
select @i as userId, @i as rankId into #a
while @i<60
begin
set @i = @i + 1
insert into #a values(@i,@i)
end

select * FROM #a where rankid <50 and (rankid+1) %3=0 order by rankid desc


结果:
userId rankId
47 47
44 44
41 41
38 38
35 35
32 32
29 29
26 26
23 23
20 20
17 17
14 14
11 11
8 8
5 5
2 2 --------------------编程问答-------------------- @i as rankId into #a

这个into #a是什么意思哈?谢谢 --------------------编程问答--------------------
引用 10 楼  的回复:
@i as rankId into #a

这个into #a是什么意思哈?谢谢

其实是select ... into ...语法 #a是自定义的表明 --------------------编程问答--------------------
引用 9 楼  的回复:
SQL code

declare @i int
set @i = 1
select @i as userId, @i as rankId into #a
while @i<60
begin
    set @i = @i + 1
    insert into #a values(@i,@i)
end

select * FROM #a where rankid <50 a……


谢了,,这个就OK了。 --------------------编程问答-------------------- select ... into ...语法   #a就是临时表,对吧,谢谢哈 --------------------编程问答--------------------

  int[] numbers = new int[11] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            var numQuery =
                from num in numbers
                where (num % 3) == 0 && num<9
                select num;

            foreach (int num in numQuery)
            {
                Console.Write("{0} ", num);
            }


顺便用linq 试试   --------------------编程问答-------------------- declare @rank int =50  ---初始数量 爱多少多少
declare @arr varchar(300) ----47,44....条件
declare @sql varchar(2000) ----sql语句
set @arr=''
while(1=1)
begin
set @rank=@rank-3
if(@rank<0)
begin
set @arr=SUBSTRING(@arr,1,(len(@arr)-1))
break
end
set @arr=@arr+ltrim(@rank)+','
end

set @sql='select * from A where rank in ('+@arr+')'
--print @sql
exec(@sql) --------------------编程问答-------------------- select * FROM #a where rankid <50 and (rankid+1) %3=0 order by rankid desc
--------------------编程问答--------------------
引用 9 楼  的回复:
SQL code


declare @i int
set @i = 1
select @i as userId, @i as rankId into #a
while @i<60
begin
    set @i = @i + 1
    insert into #a values(@i,@i)
end

select * FROM #a where rankid <50……

就是它了,
呵呵,
--------------------编程问答--------------------
引用 4 楼  的回复:
不是啊,假如当前用户的排名50,需要查出的排名单位间隔是3 ,那么我就需要把在该用户前面的,并且用户用当前的排名减去单位间隔,就是47,44,41,38,35。。。。。

但是不知道怎么写。


select * from A where rank<50 and (50-rank)%3=0

需要那么难吗??楼上各位
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,