[sql server]后台分页,震惊!最后一种竟然...

闲的没事想练习一下

要分页的数据:

select * from 表1

给她而设置个别名,为这个结果集的最前面加上序号

select 
        row_number()over(order by 表1.id asc) as numberId
        ,表1.* 
from (
        select * from 表1
       ) as 表1

最后选出前20

select top 20 biao1.* 
  from (
        select 
                row_number()over(order by 表1.id asc) as numberId
                ,表1.* 
          from (
                select * from 表1
               ) as 表1
        )as biao1
----------------------------------------------------------------------------
-- 选出了前20个,但是如果想分页就得限制更多
select biao1.* 
  from (
        select 
                row_number()over(order by 表1.id asc) as numberId
                ,表1.* 
          from (
                select * from 表1
               ) as 表1
        )as biao1 
where biao1 .numberId between 0 and 20
-- 这个0和20就可以是一个变量

但是感觉现在写的有点麻烦就简化一下

select
  biao1.* 
from
  ( 
    select
      row_number() over (order by 表1.id asc) as numberId
      , 表1.* 
    from
      表1
  ) as biao1
where
  numberId between 0 and 20

但是我还是想再简单一点,但是这么写就报错了,因为执行的from>where>gorup by>having>select|top|distinct >order by

select
  row_number() over (order by 表1.id asc) as numberId
  , 表1.* 
from
  表1
where
  numberId between 0 and 20

在和这个时候numberId还不存在,所以就报错了,目前只想到这么写。

select biao1.*
from 
(select
  row_number() over (order by 表1.id asc) as numberId
  , 表1.* 
from
  表1)as biao1
where
  numberId between 0 and 20

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值