sql 分页语句如何拼写?
my sql 分页语句如何拼写?
sql server 分页语句如何拼写?
DECLARE @i INT=3;
DECLARE @size INT=20;
-- sql
SELECT TOP(@size) * FROM ( SELECT TOP(@size*@i) * FROM TB ORDER BY id asc ) ORDER BY id DESC --可能sql里面要拼一下@i @size
-- mysql
SELECT * FROM TB ORDER BY id limit @i-1,@size
--mssql 05-08
;WITH cte AS (
SELECT rn=ROW_NUMBER() OVER(ORDER BY id ),* FROM TB
)
SELECT * FROM cte WHERE rn BETWEEN @size*(@i-1)+1 AND @size*@i;
--mssql 2012+
SELECT * FROM TB offset @size*(@i-1)+1 ROW FETCH NEXT @size ROWS ONLY;
大概,具体可能要调试
rownumber,现在的数据库好像都支持了