• limit offset,n揭秘


    今天利用limit offset,n从一个表中分批导出数据的时候,总比原表少那么几条记录,创建一个小表做测试才发现其中的奥秘:

    mysql> select * from sss1;
    +----+------+------+
    | id | name | c_id |
    +----+------+------+
    | 1 | aaa | 1 |
    | 2 | bbb | 2 |
    | 3 | ccc | 3 |
    | 4 | ddd | 1 |
    | 5 | eee | 4 |
    | 6 | ffff | 6 |
    | 7 | NULL | 0 |
    | 8 | NULL | 0 |
    | 9 | NULL | 0 |
    | 10 | ssss | 0 |
    | 11 | ooo | 0 |
    | 12 | 234 | 0 |
    +----+------+------+
    12 rows in set (0.00 sec)

    mysql
    > select * from sss1 limit 0,3;
    +----+------+------+
    | id | name | c_id |
    +----+------+------+
    | 1 | aaa | 1 |
    | 2 | bbb | 2 |
    | 3 | ccc | 3 |
    +----+------+------+
    3 rows in set (0.00 sec)

    mysql
    > select * from sss1 limit 4,6;
    +----+------+------+
    | id | name | c_id |
    +----+------+------+
    | 5 | eee | 4 |
    | 6 | ffff | 6 |
    | 7 | NULL | 0 |
    | 8 | NULL | 0 |
    | 9 | NULL | 0 |
    | 10 | ssss | 0 |
    +----+------+------+
    6 rows in set (0.00 sec)

    看明白了吗?

    如果有offset的话,就会从offset+1条开始显示,越过第offset条,所以分批后我才会少几条记录,刚好是我的offset点,解决办法:offset 和上一条的n保持一致就行了:

    mysql> select * from sss1;
    +----+------+------+
    | id | name | c_id |
    +----+------+------+
    | 1 | aaa | 1 |
    | 2 | bbb | 2 |
    | 3 | ccc | 3 |
    | 4 | ddd | 1 |
    | 5 | eee | 4 |
    | 6 | ffff | 6 |
    | 7 | NULL | 0 |
    | 8 | NULL | 0 |
    | 9 | NULL | 0 |
    | 10 | ssss | 0 |
    | 11 | ooo | 0 |
    | 12 | 234 | 0 |
    +----+------+------+
    12 rows in set (0.00 sec)

    mysql
    > select * from sss1 limit 0,3;
    +----+------+------+
    | id | name | c_id |
    +----+------+------+
    | 1 | aaa | 1 |
    | 2 | bbb | 2 |
    | 3 | ccc | 3 |
    +----+------+------+
    3 rows in set (0.00 sec)

    mysql
    > select * from sss1 limit 3,6;
    +----+------+------+
    | id | name | c_id |
    +----+------+------+
    | 4 | ddd | 1 |
    | 5 | eee | 4 |
    | 6 | ffff | 6 |
    | 7 | NULL | 0 |
    | 8 | NULL | 0 |
    | 9 | NULL | 0 |
    +----+------+------+
    6 rows in set (0.00 sec)

  • 相关阅读:
    查看执行SQL效果,消耗资源的SQL查看命令
    网页上搜索apk链接工具
    PL/SQL 获取一个月的天数排除周六周日
    java 数字转换
    jQuery选择器
    Oracle 10g 透明网关 sql server2005 配置
    怎么进行软件测试才能把bug降到最低呢??
    Demon推荐的Blog和网站
    基于.NET的俄罗斯方块课程设计
    【读书笔记】《锋利jQuery》第一章
  • 原文地址:https://www.cnblogs.com/sunss/p/2049618.html
Copyright © 2020-2023  润新知