Mysql的limit用法

Mysql的limit用法
limit a,b;
a 表示偏移量(省略时表示为偏移量为0);b 表示要查询的数量
例子:

mysql> select students.sname,subjects.stitle,scores.score from scores
    -> inner join students on scores.stuid=students.id
    -> inner join subjects on scores.subid=subjects.id
    -> order by scores.score desc;
+-------+-------------+-------+
| sname | stitle      | score |
+-------+-------------+-------+
| 张三  | Physics     | 99.00 |
| 李四  | Physics     | 98.00 |
| 李四  | Chinese     | 95.00 |
| 张三  | Chinese     | 95.00 |
| 周钱  | mathematics | 91.00 |
| 吴时  | mathematics | 91.00 |
| 李四  | english     | 90.00 |
| 周钱  | english     | 90.00 |
| 吴时  | english     | 90.00 |
| 王康  | Chinese     | 90.00 |
| 王康  | Physics     | 90.00 |
| 吴钱  | Physics     | 90.00 |
| 李四  | mathematics | 89.00 |
| 王康  | english     | 88.00 |
| 张三  | english     | 88.00 |
| 张三  | mathematics | 88.00 |
| 王康  | mathematics | 86.00 |
| 吴用  | Chinese     | 82.00 |
| 吴时  | Physics     | 81.00 |
| 吴用  | english     | 81.00 |
| 吴用  | mathematics | 81.00 |
| 周钱  | Physics     | 80.00 |
| 吴钱  | english     | 80.00 |
| 吴钱  | mathematics | 80.00 |
| 张康  | mathematics | 79.00 |
| 张康  | Chinese     | 79.00 |
| 张康  | english     | 78.00 |
| 吴时  | Chinese     | 71.00 |
| 吴用  | Physics     | 70.00 |
| 吴钱  | Chinese     | 67.00 |
| 张康  | Physics     | 59.00 |
| 周钱  | Chinese     | 50.00 |
+-------+-------------+-------+

共32条数据

mysql> select students.sname,subjects.stitle,scores.score from scores
    -> inner join students on scores.stuid=students.id
    -> inner join subjects on scores.subid=subjects.id
    -> order by scores.score desc
    -> limit 5,10;
+-------+-------------+-------+
| sname | stitle      | score |
+-------+-------------+-------+
| 吴时  | mathematics | 91.00 |
| 李四  | english     | 90.00 |
| 周钱  | english     | 90.00 |
| 吴时  | english     | 90.00 |
| 王康  | Chinese     | 90.00 |
| 王康  | Physics     | 90.00 |
| 吴钱  | Physics     | 90.00 |
| 李四  | mathematics | 89.00 |
| 王康  | english     | 88.00 |
| 张三  | english     | 88.00 |
+-------+-------------+-------+

表示从第5条往后开始搜索10条
在分页情况下
已知:每页显示 m 条数据,当前显示第 n 页
求总页数:此段逻辑后面会在 python 中实现
1 查询总条数 p1
2 使用 p1 除以 m 得到 p2
3 如果整除则 p2 为总数页
4 如果不整除则 p2+1 为总页数
求第 n 页的数据:
select * from students
limit (n-1)*m,m

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值