sql如何查询表的第一条记录和最后一条记录

转载自http://blog.csdn.net/lanyang123456/article/details/55806160

Oracle 查询出来的数据取第一条

select * from (select * from <table> order by <key>) where rownum=1;

select * from (select * from <table> order by <key> desc) where rownum=1;

sql如何查询表的第一条记录和最后一条记录

方法一:使用top

select TOP 1 * from apple;
select TOP 1 * from apple order by id desc;

(备注:top是Access的语法,MySQL不支持)

方法二:使用LIMIT

第一条记录

mysql> select * from apple LIMIT 1;

 

默认升序,等价于

mysql> select * from apple order by asc id LIMIT 1;

 

最后一条记录

mysql> select * from apple order by id desc LIMIT 1;

本文来源:https://blog.csdn.net/u014529963/article/details/79121864


如果给你带来帮助,欢迎微信或支付宝扫一扫,赞一下。