{"id":5627,"date":"2024-11-14T08:30:00","date_gmt":"2024-11-14T00:30:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5627\/"},"modified":"2024-11-14T08:30:00","modified_gmt":"2024-11-14T00:30:00","slug":"sql%e5%88%86%e9%a1%b5%e6%9f%a5%e8%af%a2%e8%af%ad%e5%8f%a5%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5627\/","title":{"rendered":"sql\u5206\u9875\u67e5\u8be2\u8bed\u53e5\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  sql \u5206\u9875\u67e5\u8be2\u4f7f\u7528 limit \u548c offset \u5b50\u53e5\uff0climit \u6307\u5b9a\u6bcf\u9875\u663e\u793a\u884c\u6570\uff0coffset \u6307\u5b9a\u4ece\u7b2c\u51e0\u884c\u5f00\u59cb\u8fd4\u56de\u6570\u636e\u3002\u7528\u6cd5\u793a\u4f8b\uff1aselect * from table_name limit n offset m\u3002\u5e94\u7528\u573a\u666f\uff1a\u5c55\u793a\u5927\u91cf\u7ed3\u679c\u3001\u5b9e\u73b0\u9875\u9762\u5bfc\u822a\u3001\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002\u6ce8\u610f\u4e8b\u9879\uff1aoffset \u4e0d\u80fd\u4e3a\u8d1f\u6570\uff0climit \u548c offset \u5fc5\u987b\u4e3a\u6574\u6570\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053019181831918.jpg\" class=\"aligncenter\" title=\"sql\u5206\u9875\u67e5\u8be2\u8bed\u53e5\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"sql\u5206\u9875\u67e5\u8be2\u8bed\u53e5\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u5206\u9875\u67e5\u8be2\u8bed\u53e5\u8be6\u89e3<\/strong><\/p>\n<p>\u5206\u9875\u67e5\u8be2\u662f\u6570\u636e\u67e5\u8be2\u4e2d\u5e38\u7528\u7684\u64cd\u4f5c\uff0c\u5b83\u53ef\u4ee5\u5c06\u67e5\u8be2\u7ed3\u679c\u5206\u6210\u591a\u4e2a\u9875\u9762\uff0c\u65b9\u4fbf\u7528\u6237\u67e5\u770b\u548c\u64cd\u4f5c\u3002SQL \u4e2d\u5206\u9875\u67e5\u8be2\u7684\u5b9e\u73b0\u9700\u8981\u7528\u5230 LIMIT \u548c OFFSET \u5b50\u53e5\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT column_list\nFROM table_name\nLIMIT n\nOFFSET m;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u53c2\u6570\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>n\uff1a\u6307\u5b9a\u8981\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u8fd4\u56de\u7684\u884c\u6570\uff0c\u5b83\u8868\u793a\u6bcf\u9875\u663e\u793a\u7684\u884c\u6570\u3002<\/li>\n<li>m\uff1a\u6307\u5b9a\u8981\u8df3\u8fc7\u7684\u884c\u6570\uff0c\u5b83\u8868\u793a\u8981\u4ece\u7b2c\u51e0\u884c\u5f00\u59cb\u8fd4\u56de\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u67e5\u8be2 users \u8868\uff0c\u663e\u793a\u524d 10 \u6761\u8bb0\u5f55\uff1a<\/p>\n<pre>SELECT *\nFROM users\nLIMIT 10\nOFFSET 0;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8df3\u8fc7\u524d 10 \u6761\u8bb0\u5f55\uff0c\u663e\u793a\u7b2c 11-20 \u6761\u8bb0\u5f55\uff1a<\/p>\n<pre>SELECT *\nFROM users\nLIMIT 10\nOFFSET 10;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cLIMIT \u548c OFFSET \u5b50\u53e5\u7684\u4f4d\u7f6e\u4e0d\u80fd\u4e92\u6362\u3002LIMIT \u5fc5\u987b\u7d27\u8ddf\u5728 OFFSET \u4e4b\u540e\u3002<\/p>\n<p><strong>\u5206\u9875\u67e5\u8be2\u7684\u5e94\u7528\uff1a<\/strong><\/p>\n<p>\u5206\u9875\u67e5\u8be2\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u573a\u666f\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u5c55\u793a\u5927\u91cf\u7684\u67e5\u8be2\u7ed3\u679c\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u5bfc\u81f4\u9875\u9762\u52a0\u8f7d\u7f13\u6162\u3002<\/li>\n<li>\u5b9e\u73b0\u9875\u9762\u5bfc\u822a\uff0c\u5141\u8bb8\u7528\u6237\u5728\u4e0d\u540c\u7684\u9875\u9762\u4e4b\u95f4\u5207\u6362\u3002<\/li>\n<li>\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\uff0c\u56e0\u4e3a\u5206\u9875\u67e5\u8be2\u53ea\u4f1a\u52a0\u8f7d\u7528\u6237\u5f53\u524d\u67e5\u770b\u7684\u9875\u9762\u6570\u636e\uff0c\u907f\u514d\u52a0\u8f7d\u5927\u91cf\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>OFFSET \u503c\u4e0d\u80fd\u4e3a\u8d1f\u6570\u3002<\/li>\n<li>LIMIT \u548c OFFSET \u503c\u90fd\u5fc5\u987b\u4e3a\u6574\u6570\u3002<\/li>\n<li>\u4e00\u4e9b\u6570\u636e\u5e93\u7cfb\u7edf\uff08\u5982 MySQL\uff09\u652f\u6301\u4f7f\u7528 Row_Number \u51fd\u6570\u5b9e\u73b0\u5206\u9875\u67e5\u8be2\uff0c\u8bed\u6cd5\u6709\u6240\u4e0d\u540c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u5206\u9875\u67e5\u8be2\u8bed\u53e5\u600e\u4e48\u5199\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>sql \u5206\u9875\u67e5\u8be2\u4f7f\u7528 limit \u548c offset \u5b50\u53e5\uff0climit \u6307\u5b9a\u6bcf\u9875\u663e\u793a\u884c\u6570\uff0coffset \u6307\u5b9a\u4ece\u7b2c\u51e0\u884c\u5f00\u59cb\u8fd4\u56de\u6570\u636e\u3002\u7528\u6cd5\u793a\u4f8b\uff1aselect * from table_name limit n offset m\u3002\u5e94\u7528\u573a\u666f\uff1a\u5c55\u793a\u5927\u91cf\u7ed3\u679c\u3001\u5b9e\u73b0\u9875\u9762\u5bfc\u822a\u3001\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002\u6ce8\u610f\u4e8b\u9879\uff1aoffset \u4e0d\u80fd\u4e3a\u8d1f\u6570\uff0climit \u548c offset \u5fc5\u987b\u4e3a\u6574\u6570\u3002 SQL \u5206\u9875\u67e5\u8be2\u8bed\u53e5\u8be6\u89e3 \u5206\u9875\u67e5\u8be2\u662f\u6570\u636e\u67e5\u8be2\u4e2d\u5e38\u7528\u7684\u64cd\u4f5c\uff0c\u5b83\u53ef\u4ee5\u5c06\u67e5\u8be2\u7ed3\u679c\u5206\u6210\u591a\u4e2a\u9875\u9762\uff0c\u65b9\u4fbf\u7528\u6237\u67e5\u770b\u548c\u64cd\u4f5c\u3002SQL \u4e2d\u5206\u9875\u67e5\u8be2\u7684\u5b9e\u73b0\u9700\u8981\u7528\u5230 LIMIT \u548c OFFSET \u5b50\u53e5\u3002 \u8bed\u6cd5\uff1a SELECT column_list FROM table_name LIMIT n OFFSET m; \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570\u8bf4\u660e\uff1a n\uff1a\u6307\u5b9a\u8981\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u8fd4\u56de\u7684\u884c\u6570\uff0c\u5b83\u8868\u793a\u6bcf\u9875\u663e\u793a\u7684\u884c\u6570\u3002 m\uff1a\u6307\u5b9a\u8981\u8df3\u8fc7\u7684\u884c\u6570\uff0c\u5b83\u8868\u793a\u8981\u4ece\u7b2c\u51e0\u884c\u5f00\u59cb\u8fd4\u56de\u6570\u636e\u3002 \u793a\u4f8b\uff1a \u67e5\u8be2 users \u8868\uff0c\u663e\u793a\u524d 10 \u6761\u8bb0\u5f55\uff1a SELECT * FROM users LIMIT 10 OFFSET 0; \u767b\u5f55\u540e\u590d\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5627","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5627"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5627\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}