﻿{"id":983,"date":"2015-03-21T21:12:29","date_gmt":"2015-03-21T13:12:29","guid":{"rendered":"http:\/\/zerobox.org\/notes\/?p=983"},"modified":"2016-06-02T19:37:06","modified_gmt":"2016-06-02T11:37:06","slug":"%e5%88%86%e9%a1%b5%e6%9f%a5%e8%af%a2%e7%9a%84%e9%82%a3%e4%ba%9b%e5%9d%91%e5%92%8c%e5%90%84%e7%a7%8d%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"http:\/\/zerobox.org\/notes\/983.html","title":{"rendered":"\u5206\u9875\u67e5\u8be2\u7684\u90a3\u4e9b\u5751\u548c\u5404\u79cd\u6280\u5de7"},"content":{"rendered":"<p>\u6765\u6e90:http:\/\/www.jamespan.me\/blog\/2015\/01\/22\/trick-of-paging-query\/<\/p>\n<p>&nbsp;<\/p>\n<h1 id=\"\u80cc\u666f\">\u80cc\u666f<\/h1>\n<p>\u4ece\u4e0a\u5468\u5f00\u59cb\u6211\u5c31\u4e00\u76f4\u5728\u505a\u6570\u636e\u6e05\u6d17\u7684\u5de5\u4f5c\uff0c\u8fd9\u6b21\u7b97\u662f\u4f53\u4f1a\u5230\u4e86\u4ec0\u4e48\u53eb\u505a\u201c\u629b\u5f00\u6570\u636e\u91cf\u8c08\u5b9e\u73b0\u5c31\u662f\u800d\u6d41\u6c13\u201d\u3002<\/p>\n<p>\u6211\u8bbe\u8ba1\u65b9\u6848\u548c\u8c03\u8bd5\u4ee3\u7801\u8fde\u63a5\u7684\u90fd\u662f\u65e5\u5e38\u73af\u5883\u7684\u6570\u636e\u5e93\uff0c\u91cc\u9762\u7684\u5355\u8868\u6570\u636e\u91cf\u5728\u767e\u7ea7\uff0c\u65e0\u8bba\u6211\u600e\u4e48\u5b9e\u73b0\u90fd\u662f\u77ac\u95f4\u6d17\u5b8c\u3002\u5230\u4e86\u6027\u80fd\u6d4b\u8bd5\u7684\u65f6\u5019\u7528\u7684\u5c31\u662f\u6027\u80fd\u5e93\uff0c\u53cc 11 \u4e4b\u524d@W\u541b\u505a\u6027\u80fd\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u5f80\u91cc\u9762\u5199\u5165\u4e86 2000W \u7684\u6570\u636e\uff0c\u8db3\u591f\u6211\u6218\u4e2a\u75db\u5feb\u3002<\/p>\n<h1 id=\"\u6df1\u5751\">\u6df1\u5751<\/h1>\n<p>\u4e00\u5f00\u59cb\u7684\u65f6\u5019\uff0c\u5206\u9875\u67e5\u8be2\u7528\u7684\u662f limit \u5b50\u53e5\uff0cSQL \u8bed\u53e5\u5f62\u6001\u5982\u4e0b\u3002<\/p>\n<pre><code class=\"sql hljs\"><span class=\"hljs-operator\"><span class=\"hljs-keyword\">select<\/span> * <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-keyword\">table<\/span> <span class=\"hljs-keyword\">where<\/span> xxx <span class=\"hljs-keyword\">in<\/span> (<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>) <span class=\"hljs-keyword\">order<\/span> <span class=\"hljs-keyword\">by<\/span> id <span class=\"hljs-keyword\">limit<\/span> #offset#, <span class=\"hljs-number\">200<\/span>\r\n<\/span><\/code><\/pre>\n<p>limit \u5b50\u53e5\u7684\u4f18\u70b9\u5f88\u660e\u663e\uff0c\u7b80\u5355\u597d\u7528\u3002\u7f3a\u70b9\u5e73\u65f6\u4e0d\u663e\u8457\uff0c\u6570\u636e\u91cf\u4e00\u5927\u5c31\u66b4\u9732\u4e86\u3002\u6570\u636e\u5e93\u4f1a\u5b8c\u6574\u626b\u63cf offset \u7684\u884c\uff0c\u7136\u540e\u7ee7\u7eed\u626b\u63cf 200 \u884c\u4e4b\u540e\u624d\u628a\u7ed3\u679c\u96c6\u8fd4\u56de\u3002 offset \u5728 400W \u7684\u65f6\u5019\uff0c\u8fd9\u6837\u7684 SQL \u505a\u4e00\u6b21\u5206\u9875\u67e5\u8be2\u5c31\u5df2\u7ecf\u81f3\u5c11\u8017\u65f6 5s \u4e86\u3002<\/p>\n<h1 id=\"\u6323\u624e\">\u6323\u624e<\/h1>\n<p>\u987f\u65f6\u611f\u89c9\u81ea\u5df1\u9677\u5165\u4e86\u4e00\u4e2a\u6ce5\u6c34\u5751\uff0c\u8d76\u7d27\u627e\u8001\u53f8\u673a\u8bf7\u6559\u3002@Y\u541b\u7ed9\u4e86\u6211\u4e00\u4e2a\u65b9\u6848\uff0c\u4e0d\u4f7f\u7528 limit\uff0c\u76f4\u63a5\u4f7f\u7528\u4e3b\u952e\u7d22\u5f15 + \u5de6\u53f3\u8303\u56f4\u67e5\u627e\uff0cSQL \u8bed\u53e5\u5f62\u6001\u5982\u4e0b\u3002<\/p>\n<pre><code class=\"sql hljs\"><span class=\"hljs-operator\"><span class=\"hljs-keyword\">select<\/span> * <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-keyword\">table<\/span> <span class=\"hljs-keyword\">where<\/span> xxx <span class=\"hljs-keyword\">in<\/span> (<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>) <span class=\"hljs-keyword\">and<\/span> id &gt;= #minId# <span class=\"hljs-keyword\">and<\/span> id &lt; #maxId#\r\n<\/span><\/code><\/pre>\n<p>\u5176\u4e2d minId \u548c maxId \u7531\u6211\u7684\u4ee3\u7801\u7ed9\u51fa\uff0c\u542f\u52a8\u65f6\u5148\u7b97\u51fa\u5f53\u524d\u8868\u7684 id \u8303\u56f4\uff0c\u7136\u540e\u5728\u6b64\u8303\u56f4\u5185\u4ee5 200 \u4e3a\u6b65\u957f\u5206\u9875\u8bfb\u53d6\uff0c\u5373 maxId &#8211; minId = 200\uff0c\u6709\u53ef\u80fd\u8bfb\u4e0d\u5230\u6570\u636e\uff0c\u6709\u53ef\u80fd\u8bfb\u5230 200 \u6761\u3002<\/p>\n<p>\u5728\u6570\u636e\u5e93\u91cc\u9762\u8bd5\u7740\u8dd1\u4e86\u51e0\u6761\u8fd9\u6837\u7684\u67e5\u8be2\uff0c\u679c\u7136\u6548\u7387\u9ad8\u4e86\u5f88\u591a\u3002\u8d76\u7d27\u542d\u54e7\u542d\u54e7\u5728\u4ee3\u7801\u4e2d\u5b9e\u73b0\u8fd9\u79cd\u5206\u9875\u903b\u8f91\uff0c\u4fe1\u5fc3\u6ee1\u6ee1\u60f3\u8981\u6027\u80fd\u98d9\u5347\uff0c\u7ed3\u679c\u3002\u3002\u3002<\/p>\n<p>\u5728\u65e5\u5e38\u6570\u636e\u5e93\u6d4b\u8bd5\u7684\u65f6\u5019\u5c31\u53d1\u73b0\u95ee\u9898\u4e86\u3002\u6211\u7684\u6570\u636e\u5e93\u7684 id \u4e0d\u662f\u7531 MySQL \u81ea\u52a8\u751f\u6210\u7684\u8fde\u7eed\u7684\u81ea\u589e\u4e3b\u952e\uff0c\u800c\u662f\u901a\u8fc7\u5176\u4ed6\u4e2d\u95f4\u4ef6\u4ea7\u751f\u7684\uff0c\u4ece\u6574\u4f53\u6765\u770b\uff0c\u6574\u4e2a id \u7684\u5206\u5e03\u6bd4\u8f83\u79bb\u6563\uff0c\u6b65\u957f 200 \u7684\u65f6\u5019\uff0c\u4e00\u6b21\u67e5\u8be2\u6839\u672c\u67e5\u4e0d\u51fa\u51e0\u6761\u6570\u636e\u3002\u5982\u679c\u6574\u5f20\u8868\u7684 maxId \u6bd4 minId \u5927\u51fa\u5f88\u591a\u5f88\u591a\uff0c\u4f1a\u4ea7\u751f\u5f88\u591a\u6b21\u65e0\u610f\u4e49\u7684\u67e5\u8be2\u3002\u8981\u60f3\u6709\u6bd4\u8f83\u597d\u7684\u547d\u4e2d\u7387\uff0c\u8fd8\u9700\u8981\u5173\u5fc3\u8868\u91cc\u9762 id \u7684\u5206\u5e03\uff0c\u6839\u636e\u5206\u5e03\u60c5\u51b5\u8c03\u6574\u6b65\u957f\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u6211\u5c31\u5728 limit \u548c min-max-id \u4e24\u79cd\u5206\u9875\u65b9\u6848\u4e4b\u95f4\u7ea0\u7ed3\uff0c\u5f00\u59cb\u53bb\u5206\u6790\u7ebf\u4e0a\u8868\u7684\u6570\u636e\u5206\u5e03\uff0c\u7136\u540e\u8003\u8651\u628a\u5927\u8868\u548c\u5c0f\u8868\u533a\u5206\u5bf9\u5f85\uff0c\u4f7f\u7528\u4e0d\u540c\u7684\u5206\u9875\u7b56\u7565\u7b49\u7b49\u3002<\/p>\n<h1 id=\"\u7075\u5149\">\u7075\u5149<\/h1>\n<p>\u767d\u5929\u88ab\u5206\u9875\u67e5\u8be2\u7684\u6027\u80fd\u6ce5\u6f6d\u5f04\u5f97\u5fc3\u529b\u6194\u60b4\uff0c\u665a\u4e0a\u56de\u5bb6\u7684\u8def\u4e0a\u51b7\u98ce\u5439\u7740\u5934\u8111\u7a0d\u5fae\u6e05\u9192\u4e00\u4e9b\uff0c\u60f3\u5230\u4e00\u4e2a\u6761\u4f3c\u4e4e\u53ef\u884c\u7684 SQL \u8bed\u53e5\u3002<\/p>\n<pre><code class=\"sql hljs\"><span class=\"hljs-operator\"><span class=\"hljs-keyword\">select<\/span> * <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-keyword\">table<\/span> <span class=\"hljs-keyword\">where<\/span> id &gt;= #minId# <span class=\"hljs-keyword\">and<\/span> xxx <span class=\"hljs-keyword\">in<\/span> (<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>) <span class=\"hljs-keyword\">limit<\/span> <span class=\"hljs-number\">200<\/span>\r\n<\/span><\/code><\/pre>\n<p>\u56de\u5230\u5bb6\u4e4b\u540e\u8feb\u4e0d\u53ca\u5f85\u5f00\u59cb\u5199\u4ee3\u7801\uff0c\u5148\u628a\u8fd9\u6761 SQL \u6362\u7740\u53c2\u6570\u5728\u6570\u636e\u5e93\u91cc\u9762\u4e00\u904d\u904d\u6267\u884c\uff0c\u611f\u89c9\u8fd9\u79cd\u5206\u9875\u65b9\u5f0f\u5b8c\u5168\u7b26\u5408\u6211\u7684\u8981\u6c42\uff0c\u67e5\u8be2\u4f7f\u7528\u7684\u4e3b\u952e\u7d22\u5f15\uff0c\u867d\u7136\u4ece\u6267\u884c\u8ba1\u5212\u770b\u5f71\u54cd\u884c\u6570\u5728\u767e\u4e07\u7ea7\uff0c\u4f46\u662f\u5b9e\u9645\u6267\u884c\u7684\u65f6\u5019\u5f71\u54cd\u884c\u6570\u4e0d\u8fc7\u767e\u7ea7\uff0c\u8fd8\u4e0d\u9700\u8981\u8003\u8651 id \u7684\u5206\u5e03\uff0c\u6bcf\u6b21\u90fd\u80fd\u5b9e\u6253\u5b9e\u7684\u7ed9\u6211\u635e\u51fa 200 \u6761\u6570\u636e\u3002<\/p>\n<p>\u4e0d\u8fc7\u4f7f\u7528\u8fd9\u6837\u7684 SQL \u8bed\u53e5\u505a\u5206\u9875\uff0c\u9700\u8981\u6ce8\u610f\u8c03\u6574 minId \u7684\u503c\uff0c\u626b\u8868\u8fc7\u7a0b\u4e2d\u9700\u8981\u505a\u5230\u4e0d\u91cd\u4e0d\u6f0f\u3002\u4e00\u79cd\u53ef\u884c\u7684\u65b9\u6848\u662f\u5c06\u672c\u6b21\u67e5\u51fa\u7684\u7ed3\u679c\u96c6\u4e2d\u7684\u6700\u5927\u7684 id\uff0c\u81ea\u589e 1 \u540e\u4f5c\u4e3a\u4e0b\u6b21\u67e5\u8be2\u7684 minId\u3002<\/p>\n<pre><code class=\"python hljs\">max_id, min_id = select min(id), max(id) <span class=\"hljs-keyword\">from<\/span> table\r\n<span class=\"hljs-keyword\">while<\/span> min_id &lt;= max_id:\r\n    objs = select * <span class=\"hljs-keyword\">from<\/span> table where id &gt;= min_id <span class=\"hljs-keyword\">and<\/span> xxx <span class=\"hljs-keyword\">in<\/span> (<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>) limit <span class=\"hljs-number\">200<\/span>\r\n    max_id_in_page = max(map(<span class=\"hljs-keyword\">lambda<\/span> x: x.id, objs))\r\n    min_id = max_id_in_page + <span class=\"hljs-number\">1<\/span>\r\n<\/code><\/pre>\n<p>\u6574\u4f53\u6765\u8bf4\uff0c\u8fd9\u79cd\u5206\u9875\u65b9\u5f0f\u907f\u514d\u4e86\u4f7f\u7528 limit \u65f6\u5019\u904d\u5386 offset \u5e26\u6765\u7684\u65e0\u8c13\u7684\u6027\u80fd\u5f00\u9500\uff0c\u907f\u514d\u4e86\u5bf9 id \u4f7f\u7528\u5de6\u53f3\u8303\u56f4\u67e5\u8be2\u65f6\u5019 id \u7684\u79bb\u6563\u5206\u5e03\u5bf9\u547d\u4e2d\u7387\u7684\u5f71\u54cd\uff0c\u4ee3\u4ef7\u662f\u9700\u8981\u5728\u5185\u5b58\u4e2d\u904d\u5386\u7ed3\u679c\u96c6\u83b7\u53d6\u5f53\u524d\u5206\u9875\u4e2d id \u7684\u6700\u5927\u503c\uff0c\u5c40\u9650\u662f\u53ea\u80fd\u5728\u5bf9\u5168\u8868\u552f\u4e00\u7684\u5b57\u6bb5\u505a\u5206\u9875\u65f6\u4f7f\u7528\u3002<\/p>\n<h1 id=\"\u6269\u5c55\">\u6269\u5c55<\/h1>\n<p>\u5173\u4e8e\u5206\u9875\u67e5\u8be2\uff0c\u4e0d\u540c\u7684\u6570\u636e\u5e93\u5728\u505a offset \u7684\u65f6\u5019\u8bed\u6cd5\u4e5f\u4e0d\u592a\u4e00\u6837\uff0c\u6bd4\u5982 M$ \u5bb6\u7684\u8bed\u6cd5\u5c31\u662f TOP n\uff0cMySQL \u7684\u8bed\u6cd5\u5c31\u662f limit n\u3002<\/p>\n<p>\u7f51\u4e0a\u7684\u5f88\u591a\u535a\u5ba2\u90fd\u63d0\u5230\u4e00\u79cd\u5229\u7528\u5b50\u67e5\u8be2\u6765\u63d0\u9ad8\u6027\u80fd\u7684\u505a\u6cd5\uff0c\u5927\u4f53\u7684\u610f\u601d\u662f\u5148\u4f7f\u7528\u666e\u901a\u7684 offset \u8bed\u6cd5\u83b7\u53d6\u76ee\u6807\u5206\u9875\u7684\u8bb0\u5f55\u7684 id \u96c6\u5408\uff0c\u518d\u6839\u636e id \u96c6\u5408\u53bb\u83b7\u53d6\u5b8c\u6574\u7684\u76ee\u6807\u6570\u636e\uff0cSQL \u8bed\u53e5\u5f62\u6001\u5982\u4e0b\u3002<\/p>\n<pre><code class=\"sql hljs\"><span class=\"hljs-operator\"><span class=\"hljs-keyword\">select<\/span> * <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-keyword\">table<\/span> <span class=\"hljs-keyword\">where<\/span> id <span class=\"hljs-keyword\">in<\/span> (<span class=\"hljs-keyword\">select<\/span> id <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-keyword\">table<\/span> <span class=\"hljs-keyword\">order<\/span> <span class=\"hljs-keyword\">by<\/span> id <span class=\"hljs-keyword\">limit<\/span> #offset#, #<span class=\"hljs-keyword\">size<\/span>#)\r\n<\/span><\/code><\/pre>\n<p>\u5176\u5b9e\u6709\u4e9b\u7248\u672c\u5c24\u5176\u4f4e\u7248\u672c\u7684 MySQL \u662f\u4e0d\u652f\u6301\u76f4\u63a5\u5bf9\u5e26 limit \u7684\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u505a in \u5b50\u53e5\u7684\uff0c\u6267\u884c\u7684\u65f6\u5019\u4f1a\u62a5\u9519\uff0c\u4fe1\u606f\u5982\u4e0b\u3002<\/p>\n<blockquote><p>This version of MySQL doesn\u2019t yet support \u2018LIMIT &amp; IN\/ALL\/ANY\/SOME subquery\u2019<\/p><\/blockquote>\n<p>\u9488\u5bf9\u8fd9\u4e2a\u95ee\u9898\uff0c\u4ed6\u4eec\u5c31\u6709\u4e86\u4e00\u4e2a\u6539\u8fdb\u4e4b\u540e\u7684\u5b50\u67e5\u8be2\u7248\u672c\u3002<\/p>\n<pre><code class=\"sql hljs\"><span class=\"hljs-operator\"><span class=\"hljs-keyword\">select<\/span> * <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-keyword\">table<\/span> <span class=\"hljs-keyword\">where<\/span> id &gt;= (<span class=\"hljs-keyword\">select<\/span> id <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-keyword\">table<\/span> <span class=\"hljs-keyword\">order<\/span> <span class=\"hljs-keyword\">by<\/span> id <span class=\"hljs-keyword\">limit<\/span> #offset#, <span class=\"hljs-number\">1<\/span>)\r\n<\/span><\/code><\/pre>\n<p>\u5176\u5b9e\u662f\u5148\u53d6\u51fa\u76ee\u6807\u5206\u9875\u7684\u7b2c\u4e00\u6761\u8bb0\u5f55\u7684id\uff0c \u7136\u540e\u6839\u636e id \u505a\u8303\u56f4\u67e5\u627e\u548c\u6761\u6570\u9650\u5236\u3002\u8fd9\u6761 SQL \u8bed\u53e5\u7684\u6548\u7387\u5728 offset \u8fbe\u5230\u767e\u4e07\u7ea7\u65f6\u76f8\u6bd4\u76f4\u63a5 limit \u6709\u6570\u500d\u7684\u63d0\u5347\uff0c\u4f46\u662f\u6ce8\u610f\u5230 MySQL \u5b50\u67e5\u8be2\u5176\u5b9e\u662f\u4e00\u4e2a\u5751\uff0c\u8fd9\u6761\u8bed\u53e5\u4e0d\u4f46\u6ca1\u6709\u907f\u514d\u904d\u5386 offset\uff0c\u8fd8\u505a\u4e86\u5927\u91cf\u7684\u65e0\u7528\u91cd\u590d\u5de5\u4f5c\u3002<\/p>\n<p>\u672c\u8d28\u4e0a\uff0c\u6211\u6700\u7ec8\u4f7f\u7528\u7684\u5206\u9875\u65b9\u6848\u662f\u5bf9\u8fd9\u6761 SQL \u8bed\u53e5\u7684\u4f18\u5316\uff0c\u501f\u52a9 id \u7684\u6709\u5e8f\u6027\u548c\u552f\u4e00\u6027\uff0c\u4f7f\u7528 <code>max(map(lambda x: x.id, objs)) + 1<\/code> \u66ff\u4ee3\u4e86\u5b50\u67e5\u8be2\u3002<\/p>\n<p>\u6709\u4eba\u4e3a\u4e86\u7ed5\u8fc7 MySQL \u4e0d\u652f\u6301\u5bf9\u5b50\u67e5\u8be2\u7ed3\u679c\u505a in \u5b50\u53e5\u7684\u9650\u5236\uff0c\u8111\u6d1e\u5927\u5f00\u5199\u51fa\u4e86\u5982\u4e0b\u67e5\u8be2\u3002<\/p>\n<pre><code class=\"sql hljs\"><span class=\"hljs-operator\"><span class=\"hljs-keyword\">select<\/span> * <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-keyword\">table<\/span> <span class=\"hljs-keyword\">where<\/span> id <span class=\"hljs-keyword\">in<\/span> (<span class=\"hljs-keyword\">select<\/span> id <span class=\"hljs-keyword\">from<\/span> (<span class=\"hljs-keyword\">select<\/span> id <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-keyword\">table<\/span> <span class=\"hljs-keyword\">order<\/span> <span class=\"hljs-keyword\">by<\/span> id <span class=\"hljs-keyword\">limit<\/span> #offset#, #<span class=\"hljs-keyword\">size<\/span>#) <span class=\"hljs-keyword\">as<\/span> tmp)\r\n<\/span><\/code><\/pre>\n<p>\u65e2\u7136\u4e0d\u5141\u8bb8\u76f4\u63a5\u5bf9\u5e26 limit \u7684\u5b50\u67e5\u8be2\u505a in\uff0c\u90a3\u4e48\u5e72\u8106\u7528\u5b50\u67e5\u8be2\u5957\u5b50\u67e5\u8be2\uff0c\u4e5f\u662f\u9189\u4e86\u3002\u8fd9\u6761 SQL \u8bed\u53e5\u7684\u6548\u7387\u8fd8\u4e0d\u5982\u76f4\u63a5 limit\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u79cd\u6539\u8fdb\u65b9\u6848\u5c31\u662f\u4f20\u8bf4\u4e2d\u7684\u201c\u67e5\u4e24\u6b21\u201d\u3002\u7b2c\u4e00\u6b21\u5148\u67e5\u51fa\u76ee\u6807\u5206\u9875\u7684 id \u96c6\u5408\uff0c\u56e0\u4e3a\u53ea\u67e5 id\uff0c\u5927\u90e8\u5206\u60c5\u51b5\u53ef\u4ee5\u76f4\u63a5\u547d\u4e2d\u7d22\u5f15\u7136\u540e\u8fd4\u56de\uff0c\u901f\u5ea6\u8fd8\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002\u7136\u540e\u7b2c\u4e8c\u6b21\u76f4\u63a5\u6839\u636e id \u96c6\u5408\u505a in \u5b50\u53e5\u67e5\u8be2\uff0c\u8d70\u7684\u4e3b\u952e\u7d22\u5f15\uff0c\u8fd9\u6b21\u5c31\u662f\u79d2\u51fa\u4e86\u3002<\/p>\n<p>\u5176\u5b9e\u67e5\u4e24\u6b21\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\u9700\u8981\u5177\u4f53\u5230\u60c5\u666f\u6765\u5206\u6790\uff0c\u4e0d\u80fd\u5f53\u505a\u662f\u4e07\u91d1\u6cb9\u3002<\/p>\n<p>\u6211\u5728\u6700\u7ec8\u4f7f\u7528\u7684\u5206\u9875\u65b9\u6848\uff0c\u5728\u6211\u7684\u5e94\u7528\u573a\u666f\u4e0b\uff0c\u6027\u80fd\u662f\u8d85\u8fc7\u4e0a\u9762\u5176\u4f59\u51e0\u79cd\u5206\u9875\u65b9\u6848\u7684\u3002<\/p>\n<h1 id=\"\u73b0\u5b9e\">\u73b0\u5b9e<\/h1>\n<p>\u5728\u73b0\u5b9e\u4e16\u754c\u7684\u5f00\u53d1\u4e2d\uff0c\u5206\u9875\u8fdc\u6bd4\u9884\u6599\u7684\u8981\u590d\u6742\u5f97\u591a\u3002<\/p>\n<p>\u5982\u679c Web \u9875\u9762\u901a\u8fc7\u8bb0\u5f55\u7684\u521b\u5efa\u65f6\u95f4\u6765\u5206\u9875\uff0c\u90a3\u4e48\u5f88\u53ef\u80fd\u6211\u6700\u7ec8\u4f7f\u7528\u7684\u5206\u9875\u65b9\u6848\u5c31\u4e0d\u80fd\u5957\u7528\u4e86\uff0c\u56e0\u4e3a\u65f6\u95f4\u4e0d\u662f\u4e00\u4e2a\u5168\u8868\u552f\u4e00\u7684\u503c\uff0c\u5f88\u96be\u5728\u4e0d\u4f7f\u7528 limit \u7684\u60c5\u51b5\u4e0b\u505a\u5230\u4e0d\u91cd\u4e0d\u6f0f\u3002\u5982\u679c\u8fd0\u6c14\u4e0d\u9519\uff0c\u4f7f\u7528\u7684\u662f\u6570\u636e\u5e93\u7684\u81ea\u589e\u4e3b\u952e\uff0c\u90a3\u4e48\u53ef\u4ee5\u8ba4\u4e3a\u4e3b\u952e\u7684\u53d8\u5316\u8d8b\u52bf\u548c\u521b\u5efa\u65f6\u95f4\u7684\u53d8\u5316\u8d8b\u52bf\u662f\u7b49\u4ef7\u7684\uff0c\u53ef\u4ee5\u5c06\u5bf9\u65f6\u95f4\u7684\u5206\u9875\u6620\u5c04\u6210\u5bf9\u4e3b\u952e\u7684\u5206\u9875\u3002<\/p>\n<p>\u5728\u5206\u9875\u7684\u4ea4\u4e92\u903b\u8f91\u4e0a\uff0c\u5929\u671d\u7684\u4ea7\u54c1\u7ecf\u7406\u4f3c\u4e4e\u504f\u597d\u4e8e\u7535\u68af\u5f0f\u7684\u5206\u9875\u63a7\u4ef6\u3002\u5176\u5b9e\u8fd8\u662f\u201c\u629b\u5f00\u6570\u636e\u91cf\u8c08\u5b9e\u73b0\u5c31\u662f\u800d\u6d41\u6c13\u201d\uff0c\u5728\u6570\u636e\u91cf\u5c0f\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u4ec0\u4e48\u6837\u7684\u5206\u9875\u65b9\u5f0f\u5bf9\u7cfb\u7edf\u6027\u80fd\u90fd\u6ca1\u4ec0\u4e48\u5f71\u54cd\uff0c\u4f46\u662f\u5728\u6570\u636e\u91cf\u5927\u5230\u4e00\u5b9a\u7a0b\u5ea6\u7684\u65f6\u5019\uff0c\u7535\u68af\u5f0f\u7684\u5206\u9875\u5bf9\u7cfb\u7edf\u6027\u80fd\u7684\u5927\u91cf\u6d88\u8017\u53cd\u800c\u4f1a\u4f24\u5bb3\u6240\u8c13\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u5173\u4e8e\u5927\u6570\u636e\u91cf\u4e0b\u7684\u5206\u9875\u5b9e\u73b0\uff0c\u4e1a\u754c\u5176\u5b9e\u5df2\u7ecf\u6709\u4e86\u597d\u51e0\u79cd\u7b56\u7565\uff0c\u57fa\u4e8e\u4e0d\u540c\u7684\u5047\u8bbe\u3002<\/p>\n<p>\u5047\u8bbe\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u8d8a\u65e9\u4ea7\u751f\u7684\u6570\u636e\uff0c\u4ef7\u503c\u8d8a\u5c0f\u3002\u5982\u679c\u5047\u8bbe\u6210\u7acb\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba4\u4e3a\u7edd\u5927\u591a\u6570\u7528\u6237\u662f\u6ca1\u6709\u8fd9\u6837\u4e00\u4e2a\u9700\u6c42\u8981\u53bb\u67e5\u770b\u4ed6\u5728\u7cfb\u7edf\u4e2d\u4ea7\u751f\u7684\u7b2c\u4e00\u6761\u6570\u636e\u7684\u3002\u5982\u679c\u7b2c\u4e00\u6761\u6570\u636e\u5bf9\u7528\u6237\u771f\u7684\u5f88\u91cd\u8981\uff0c\u5373\u4f7f\u518d\u56f0\u96be\u4ed6\u4e5f\u4f1a\u60f3\u529e\u6cd5\u5f97\u5230\u3002<\/p>\n<p>\u57fa\u4e8e\u8fd9\u6837\u7684\u5047\u8bbe\uff0c\u5728\u6570\u636e\u91cf\u8db3\u591f\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5bf9\u7cfb\u7edf\u5b9e\u73b0\u548c\u4ea4\u4e92\u4f53\u9a8c\u505a\u51fa\u5f88\u591a\u4f18\u5316\uff0c\u6bd4\u5982\u4e0d\u518d\u63d0\u4f9b\u7cbe\u786e\u7684\u7535\u68af\u5f0f\u5206\u9875\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\u4e0b\u4e00\u9875\u3001\u4e0a\u4e00\u9875\uff1b\u4e0d\u518d\u63d0\u4f9b\u7cbe\u786e\u7684\u603b\u9875\u6570\uff0c\u800c\u662f\u63d0\u4f9b\u4e00\u4e2a\u5927\u6982\u7684\u6761\u76ee\u603b\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b SQL \u7684\u6267\u884c\u8ba1\u5212\u5f97\u5230\u3002<\/p>\n<p>\u4e4b\u524d\u770b\u8fc7\u4e00\u4e2a\u5173\u4e8e\u4e0a\u4e00\u9875\u3001\u4e0b\u4e00\u9875\u7684\u5b9e\u73b0\u6280\u5de7\u3002\u5047\u5982\u6bcf\u9875\u663e\u793a 20 \u6761\u6570\u636e\uff0c\u90a3\u4e48\u67e5\u8be2\u6570\u636e\u5e93\u7684\u65f6\u5019\uff0c\u7528 <code>limit #offset#, 21<\/code> \u53d6\u51fa 21 \u6761\u8bb0\u5f55\uff0c\u9875\u9762\u5c55\u73b020\u6761\u3002\u5982\u679c\u53d6\u5230\u4e86 21 \u6761\uff0c\u8bf4\u660e\u4e0b\u4e00\u9875\u8fd8\u6709\u6570\u636e\uff0c\u5728\u9875\u9762\u5c55\u793a\u4e0b\u4e00\u9875\u6309\u94ae\u3002\u5982\u679c\u7ed3\u679c\u96c6\u6570\u91cf\u4e0d\u8db3 21\uff0c\u8bf4\u660e\u5df2\u7ecf\u5230\u4e86\u6700\u540e\u4e00\u9875\uff0c\u65e0\u9700\u663e\u793a\u4e0b\u4e00\u9875\u6309\u94ae\u4e86\u3002\u8fd9\u79cd\u65b9\u5f0f\u5b8c\u5168\u907f\u514d\u4e86\u5728\u5206\u9875\u67e5\u8be2\u65f6\u5bf9\u603b\u6761\u76ee\u6570\u91cf\u7684\u67e5\u8be2\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u79cd\u7b56\u7565\u662f\u57fa\u4e8e\u8fd9\u6837\u7684\u5047\u8bbe\uff1a\u7528\u6237\u6bd4\u8f83\u5173\u5fc3\u7684\u662f\u6700\u8fd1\u4ea7\u751f\u7684\u4e00\u5c0f\u90e8\u5206\u6570\u636e\u3002\u5728\u7528\u6237\u67e5\u8be2\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u4e00\u6b21\u6027\u4ece\u6570\u636e\u5e93\u67e5\u8be2\u7b26\u5408\u6761\u4ef6\u7684 N \u6761\u6570\u636e\u7f13\u5b58\u8d77\u6765\uff0c\u8db3\u591f\u7528\u6237\u7ffb\u4e2a\u51e0\u9875\uff0c\u8fd9\u6837\u54ea\u6015\u662f\u4f7f\u7528\u7535\u68af\u5f0f\u5206\u9875\uff0c\u5728\u8ba1\u7b97\u603b\u9875\u6570\u65f6\u4e5f\u65e0\u9700\u67e5\u8be2\u6570\u636e\u5e93\u4e86\u3002<\/p>\n<h1 id=\"\u603b\u7ed3\">\u603b\u7ed3<\/h1>\n<p>\u88ab\u5206\u9875\u95ee\u9898\u5751\u4e86\uff0c\u5176\u5b9e\u8fd8\u662f\u8bf4\u660e\u4e00\u4e2a\u95ee\u9898\uff1a\u56fe\u6837\u56fe\u68ee\u7834\uff0c\u59ff\u52bf\u6c34\u5e73\u6709\u5f85\u63d0\u9ad8\u3002\u8fd9\u6b21\u5199\u6d17\u6570\u636e\u7684\u4ee3\u7801\u7ecf\u5386\u597d\u51e0\u6b21\u65b9\u6848\u53d8\u6362\u548c\u4ee3\u7801\u91cd\u5199\uff0c\u624d\u5f97\u5230\u4e00\u4e2a\u52c9\u5f3a\u62ff\u5f97\u51fa\u624b\u7684\u7248\u672c\uff0c\u671f\u95f4\u5404\u79cd\u6da8\u59ff\u52bf\uff0c\u597d\u6709\u6536\u83b7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6765\u6e90:http:\/\/www.jamespan.me\/blog\/2015\/01\/2 &hellip;<\/p>\n<p class=\"read-more\"><a href=\"http:\/\/zerobox.org\/notes\/983.html\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[235],"class_list":["post-983","post","type-post","status-publish","format-standard","hentry","tag-235"],"views":2071,"_links":{"self":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/comments?post=983"}],"version-history":[{"count":0,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/983\/revisions"}],"wp:attachment":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/media?parent=983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/categories?post=983"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/tags?post=983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}