mysql、sqlserver、oracle获取最后⼀条数据
在⽇常项⽬中经常会遇到查询第⼀条或者最后⼀条数据的情况,针对不同数据库,我整理了mysql、sqlserver、oracle数据库的获取⽅法。
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条数据。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。