select top 3 stuid from pagesize where stuid not in (select top 3 stuid from pagesize order by id asc) order by id asc这里的分页sql语句为什么要加上排序呢?不排序不行吗?我看到很多sql语句分页代码在括号外面和里面都有排序!能具体说下什么吗?或者有其他的例子来说一下!?? id stuid 1 1 2 2 3 2 4 2 5 2 6 5 7 6 8 7 9 8 10 8 11 9 12 10//对于这个表sql语句select top 3 stuid from pagesize where stuid not in (select top 3 stuid from pagesize order by id asc) order by id asc 为什么输出的不是2,2,5呢?如果是倒序的话 括号外面和里面都要加上排序 因为是反起来的 默认排序方式是asc 你这个不加排序也是正确的 用order by是保证子查询和外面的主查询看到的是“同一张表”(记录顺序相同),这样才能达到分页的效果。
主查询的 top 3 表示选择3条记录(每页记录数),子查询的 top 3 表示跳过的记录数(页号*每页记录数)
分页查询应当用主键id,不应当用stupid,因为它有重复记录显然不是主键 输出不是2,2,5吗? SELECT TOP 页大小 * from TestTable WHERE (ID NOT IN (SELECT TOP 页大小*页数 id from 表 ORDER BY id)) ORDER BY ID