doufang8965 2018-04-20 10:27
浏览 216
已采纳

MySQLi查询中的LIMIT问题

I have three PHP APIs which have the below queries:

API 1:

$sql = "SELECT number, status, user_id from number_list WHERE disable=0 
   order by id LIMIT 0,700";

API 2:

$sql = "SELECT number, status, user_id from number_list WHERE disable=0 
   order by id LIMIT 701,1400";

API 3:

$sql = "SELECT number, status, user_id from number_list WHERE disable=0 
   order by id LIMIT 1401,2100";

I want take 700 records in each API. For example,

0 to 700 in API 1,

701 to 1400 in API 2 and

1401 to 2100 in API 3 (like shown above).

API 1 is working fine but API 2 and 3 are missing a result.

Why is this occurring and how do I correct it?

  • 写回答

3条回答 默认 最新

  • dqhbuwrwq692118284 2018-04-20 10:33
    关注

    API 1

    $sql = "SELECT number, status, user_id from number_list WHERE disable=0 
       order by id LIMIT 0,700";
    

    API 2

    $sql = "SELECT number, status, user_id from number_list WHERE disable=0 
       order by id LIMIT 700,700";
    

    API 3

    $sql = "SELECT number, status, user_id from number_list WHERE disable=0 
       order by id LIMIT 1400,700";
    

    Explanation

    Mysql also provides a way to handle this: by using OFFSET.

    The SQL query below says "return only 10 records, start on record 16 (OFFSET 15)":

    $sql = "SELECT * FROM Orders LIMIT 10 OFFSET 15";
    

    You could also use a shorter syntax to achieve the same result:

    $sql = "SELECT * FROM Orders LIMIT 15, 10";
    

    Demo

    http://sqlfiddle.com/#!9/10ff28/7

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c