首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

一条SQL话语,很简单

2011-12-20 
一条SQL语句,很简单选出一个表中的第六到第十条记录,有两个写法第一selecttop5*fromtablewhreidnotin(sele

一条SQL语句,很简单
选出一个表中的第六到第十条记录,
有两个写法第一select   top   5   *   from   table   whre   id   not   in   (select   top   5   id   from   table)
那么第二种呢。

[解决办法]
declare @cot int
declare @sql varchar(2000)
select @cot = count(1) from [table]
set @cot = @cot - 5
set @sql = 'select top 5 * from '
set @sql = @sql + ' ( select top ' + @cot + ' * from [table] desc ) a '
exec(@sql)
[解决办法]
WITH table AS
(SELECT *
ROW_NUMBER() OVER ()as RowNumber
FROM table)
SELECT *
FROM table
WHERE RowNumber between 50 and 60;
[解决办法]
declare @indextable table(id int identity(1,1),yourID int)
insert into @indextable(yourID) select fieldID from yourTable desc

select * from yourTable Y
inner join @indextable t on
Y.fieldID=t.yourID
where t.id> =6 and t.id <=10

[解决办法]
第二种写法:select top 5 from table where id in (select top 10 from table order by id )order by id desc

[解决办法]
sql2005是这样实现的.
SELECT *,
ROW_NUMBER() OVER () as RowNumber
FROM table
WHERE RowNumber between 6 and 10;

热点排行