feng-hua

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

方法一:使用top

select TOP 1 * from apple;
TOP 1 表示表apple中的第一条数据 select TOP 1 * from apple order by id desc;
TOP也是第一条数据,但是order by(以什么排序),id desc 以id的降序排列,所以要是id要是增长的话,最后一条数据就被排到第一条了

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

方法二:使用LIMIT

第一条记录

mysql> select * from apple LIMIT 1;
  • 1

默认升序,等价于

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

最后一条记录

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

分类:

技术点:

相关文章: