mysql、sqlserver、oracle获取最后一条数据

在日常项目中经常会遇到查询第一条或者最后一条数据的情况,针对不同数据库,我整理了mysql、sqlserver、oracle数据库的获取方法。

1、mysql 使用limit

select * from table order by col  limit index,rows;

表在经过col排序后,取从index+1条数据开始的rows条数据。

select * from table order by col  limit rows;

表示返还前rows条数据,相当于 limit 0,rows

select * from table order by col  limit rows,-1;

表示查询第rows+1条数据后的所有数据。

2、oracle 使用 row_number()over(partition by col1 order by col2)

select row_number()over(partition by col1 order by col2) rnm from table  rnm = 1;

表在经过col1分组后根据col2排序,row_number()over返还排序后的结果

3、sql server top

select top n * from table order by col ;

查询表的前n条数据。

select top n percent from table order by col ;

查询前百分之n条数据。

原文地址:https://www.cnblogs.com/huanlingjisi/p/12773308.html