标签:
背景:
在分页功能中,记录需分页显示,需要row_number()函数标记行号。
数据表:
排序之前数据表显示:
sql语句:
1 select ROW_NUMBER() over(order by id) as RowNum,Name,Val from T1
检索结果:
注意:Leslie的ID为Null,经过排序之后,rows却升为了首位。
常见错误:列名RowNum失效。
解释:在sql中热名称(即刚定的名称RowNum)不能马上使用,需要包一层查询。
比如说:实现分页显示,只想查询1-5行的结果,sql语句如下:
1 select ROW_NUMBER() over(order by id) as rows,Name,Val from T1 where rows between 1 and 5
解决方案:
1 select * from 2 (select ROW_NUMBER() over(order by id) as rows,Name,Val from T1) U 3 where rows between 1 and 5;
也就是:
1 with A as 2 ( 3 select ROW_NUMBER() over(order by id) as rows,Name,Val from T1 4 ) 5 select * from A where A.rows between 1 and 5;
检索结果:
参考资料:热名称
标签:
原文地址:http://www.cnblogs.com/SharpL/p/4640607.html