日期:2014-05-19  浏览次数:20724 次

简单SQL语句
假如一个A表有40条记录我怎么得到23到33之间的记录,没有编号

------解决方案--------------------
select top(33)from table where not in (select top(23))
------解决方案--------------------
select top(33)from table where not in (select top(23))

------解决方案--------------------
楼上两位真的快...
select identity(int,1,1) as 序号,usid,usname into #1 from 表
select * from #1 where 序号 BETWEEN 22 and 33
------解决方案--------------------
DECLARE @BeginIndex INT,@EndIndex INT

SET @BeginIndex = 23
SET @EndIndex = 33

BEGIN WITH TempTable AS(
SELECT *, Row_Number() OVER(ORDER BY 主键 DESC) AS RowNumber
FROM YourTable)
SELECT TempTable.*
FROM TempTable
WHERE TempTable.RowNumber BETWEEN @BeginIndex AND @EndIndex
END

看看别人分页的存储过程
------解决方案--------------------
楼上两位真的快...
select identity(int,1,1) as 序号,usid,usname into #1 from 表
select * from #1 where 序号 BETWEEN 22 and 33
-----------
这位老兄直接都给你添加编号了
想查多少都行
------解决方案--------------------
showrock(玉蝴蝶) 正解
use pubs
select identity(int,1,1) as 序号,fname into #1 from employee
select * from #1 where 序号 BETWEEN 22 and 33
------解决方案--------------------
玉蝴蝶是先给你的表添加序列号,但是如果表的数据如果太大,就会造成一点点运行速度的问题。稍微修改一下:

因为你只要选最多33条,这样可以满足条件。

select TOP 33 identity(int,1,1) as 序号,usid,usname into #1 from 表
select * from #1 where 序号 BETWEEN 22 and 33

用一条select也可以解决
select top 12 from (select top 33 from 表)
这里的12其实就是33-22+1=12(22到33一共12条纪录)
------解决方案--------------------
有点问题,修改如下
select top 12 * from (select top 33 * from 表) 临时表
------解决方案--------------------
select top 10 * from (select top 33 * from tab ) as a order by id desc
------解决方案--------------------
showrock(玉蝴蝶) 正解
select identity(int,1,1) as 序号,* into #t from table
select * from #t where 序号 BETWEEN 22 and 33

学习了.
先为其无序号表添加一个替代自动生成列并把他写入临时表T中
再从临时表T中去读取相应范围的记录,问题解决
LZ注意,两条SQL语要一起运行哈~

------解决方案--------------------
select TOP 33 identity(int,1,1) as 序号,usid,usname into #1 from A
select * from #1 where 序号 BETWEEN 22 and 33 或select top 12 * from (select top 33 * from 表) 临时表
正解!
------解决方案--------------------
select identity(int,1,1) as 序号,* into #t from table
select * from #t where 序号 BETWEEN 22 and 33
最好的方法。