假设 clause = where 1=1
假设 pagesize = 10 page = 1
也就是每页10行数据, 查询第1页
-- MYSQL 下面执行的语句:
select * from ec_order
where 1=1
order by id desc limit 0 , 10;
-- 切换到 SQL Server
select top 10 *
from ec_order
where 1=1
order by id desc
假设 pagesize = 10 page = 2
也就是每页10行数据, 查询第2页
-- MYSQL 下面执行的语句:
select * from ec_order
where 1=1
order by id desc limit 10 , 10;
-- 切换到 SQL Server 方案1.
select top 10 *
from ec_order
where 1=1 AND id NOT IN (SELECT top 10 id FROM ec_order where 1=1 order by id desc)
order by id desc
-- 切换到 SQL Server 方案2.
SELECT *
FROM
(
select ROW_NUMBER() OVER(ORDER BY id desc) AS NO, ec_order.*
from ec_order
where 1=1
)
WHERE NO between 11 and 20
-- 切换到 SQL Server 方案3.
select top 20 * from ec_order where 1=1 order by id desc
EXCEPT
select top 10 * from ec_order where 1=1 order by id desc
--第一种方式[针对数据量较小的分页统计]
drop table #tmp;
Select identity(int,1,1) as id,* into #tmp from songlist
select sourcefilename ,songname,videotype,singerid,singer,songpic from #tmp where id between 200 and 300;
select count(*) as n1,ceiling(count(*)/200) as n2 from songlist;
--第二种方式[主要针对数据量稍微大点的 分页统计 先按照主键排序]
declare @pagerow int ,@pageid int
set @pagerow = 100;--每页显示的个数
set @pageid = 2;--第几页
select top @pagerow sourcefilename, songname,videotype,singerid,singer,songpic from songlist
where sourcefilename not in (select top @pagerow*(@pageid-1) sourcefilename from songlist order by sourcefilename)
order by sourcefilename;