方法一:
代码如下:
SELECT TOP 页大小 *
FROM table1
WHERE id NOT IN
(
SELECT TOP 页大小*(页数-1) id FROM table1 ORDER BY id
)
ORDER BY id
FROM table1
WHERE id NOT IN
(
SELECT TOP 页大小*(页数-1) id FROM table1 ORDER BY id
)
ORDER BY id
方法二:
代码如下:
SELECT TOP 页大小 *
FROM table1
WHERE id >
(
SELECT ISNULL(MAX(id),0)
FROM
(
SELECT TOP 页大小*(页数-1) id FROM table1 ORDER BY id
) A
)
ORDER BY id
FROM table1
WHERE id >
(
SELECT ISNULL(MAX(id),0)
FROM
(
SELECT TOP 页大小*(页数-1) id FROM table1 ORDER BY id
) A
)
ORDER BY id
方法二倒序:
代码如下:
SELECT TOP 页大小 *
FROM table1
WHERE ID <=
(
SELECT ISNULL(MIN(ID),(SELECT MAX(ID) FROM table1 ))
FROM
(
SELECT TOP (页大小*(页数-1)) ID FROM tbl_files ORDER BY ID DESC
) A
)
ORDER BY ID DESC
FROM table1
WHERE ID <=
(
SELECT ISNULL(MIN(ID),(SELECT MAX(ID) FROM table1 ))
FROM
(
SELECT TOP (页大小*(页数-1)) ID FROM tbl_files ORDER BY ID DESC
) A
)
ORDER BY ID DESC
方法三
代码如下:
SELECT TOP 页大小 *
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY id) AS RowNumber,* FROM table1
) A
WHERE RowNumber > 页大小*(页数-1)
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY id) AS RowNumber,* FROM table1
) A
WHERE RowNumber > 页大小*(页数-1)
分页方案二:(利用ID大于多少和SELECT TOP分页)效率最高,需要拼接sql语句
分页方案一:(利用Not In和SELECT TOP分页) 效率次之,需要拼接sql语句
分页方案三:(利用sql的游标存储过程分页) 效率最差,但是最为通用PS:这三条方案是在网上搜到的,做分页的时候选用的方案二,发现直接套用语句不能满足要求,因为根据发帖顺序,是需要倒序排列的,就是最新发帖需要排到最前面,于是仿照方案二改成了倒序,希望对大家有所帮助。