{"id":6327,"date":"2024-11-14T13:22:14","date_gmt":"2024-11-14T05:22:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6327\/"},"modified":"2024-11-14T13:22:14","modified_gmt":"2024-11-14T05:22:14","slug":"mysql%e5%88%86%e9%a1%b5%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6327\/","title":{"rendered":"mysql\u5206\u9875\u600e\u4e48\u5b9e\u73b0"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\u5b9e\u73b0\u5206\u9875\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u786e\u5b9a\u6570\u636e\u8868\u3002\u6307\u5b9a\u6bcf\u9875\u884c\u6570\u3002\u8ba1\u7b97\u5f53\u524d\u9875\u7801\u3002\u4f7f\u7528 limit \u5b50\u53e5\u8bbe\u7f6e\u504f\u79fb\u91cf\u548c\u884c\u6570\uff0c\u4ece\u6307\u5b9a\u7684\u884c\u5f00\u59cb\u83b7\u53d6\u6570\u636e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202407\/31\/2024073122433734331.jpg\" class=\"aligncenter\" title=\"mysql\u5206\u9875\u600e\u4e48\u5b9e\u73b0\u63d2\u56fe\" alt=\"mysql\u5206\u9875\u600e\u4e48\u5b9e\u73b0\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u5206\u9875\u5b9e\u73b0<\/strong><\/p>\n<p><strong>\u5982\u4f55\u5b9e\u73b0 MySQL \u5206\u9875\uff1f<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\u5206\u9875\uff1a<\/p>\n<p><strong>1. \u786e\u5b9a\u8981\u5206\u9875\u7684\u6570\u636e\u8868<\/strong><br \/>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a user \u7684\u8868\uff0c\u5176\u4e2d\u5b58\u50a8\u7740\u7528\u6237\u6570\u636e\u3002<\/p>\n<p><strong>2. \u786e\u5b9a\u6bcf\u9875\u8981\u663e\u793a\u7684\u884c\u6570<\/strong><br \/>\u4f8b\u5982\uff0c\u6bcf\u9875\u663e\u793a 20 \u884c\u3002<\/p>\n<p><strong>3. \u786e\u5b9a\u5f53\u524d\u9875\u7801<\/strong><br \/>\u4f8b\u5982\uff0c\u7528\u6237\u6b63\u5728\u67e5\u770b\u7b2c 2 \u9875\u3002<\/p>\n<p><strong>4. \u4f7f\u7528 LIMIT \u5b50\u53e5<\/strong><\/p>\n<pre>SELECT * FROM user LIMIT N, M;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>N \u662f\u504f\u79fb\u91cf\uff0c\u8868\u793a\u4ece\u7b2c N \u884c\u5f00\u59cb\u83b7\u53d6\u6570\u636e\u3002<\/li>\n<li>M \u662f\u8981\u83b7\u53d6\u7684\u884c\u6570\u3002<\/li>\n<\/ul>\n<p><strong>5. \u8ba1\u7b97\u504f\u79fb\u91cf<\/strong><br \/>\u504f\u79fb\u91cf\u662f\u6bcf\u9875\u884c\u6570\u4e0e\uff08\u5f53\u524d\u9875\u7801 &#8211; 1\uff09\u7684\u4e58\u79ef\u3002<\/p>\n<pre>OFFSET = (Page Number - 1) * Row Count Per Page;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u6bcf\u9875\u663e\u793a 20 \u884c\uff0c\u5f53\u524d\u9875\u7801\u4e3a 2\uff0c\u5219\u504f\u79fb\u91cf\u4e3a (2 &#8211; 1) * 20 = 20\u3002<\/p>\n<p><strong>6. \u6267\u884c\u67e5\u8be2<\/strong><\/p>\n<pre>SELECT * FROM user LIMIT 20 OFFSET 20;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u83b7\u53d6 user \u8868\u4e2d\u4ece\u7b2c 21 \u884c\u5f00\u59cb\u7684 20 \u884c\u6570\u636e\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>SELECT * FROM user LIMIT 20 OFFSET 0;  -- \u83b7\u53d6\u7b2c 1 \u9875\nSELECT * FROM user LIMIT 20 OFFSET 20; -- \u83b7\u53d6\u7b2c 2 \u9875<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u6bcf\u9875\u884c\u6570\u662f\u56fa\u5b9a\u7684\u3002<\/li>\n<li>\u5982\u679c\u6ca1\u6709\u6307\u5b9a OFFSET \u5b50\u53e5\uff0c\u9ed8\u8ba4\u4ece\u7b2c\u4e00\u884c\u5f00\u59cb\u83b7\u53d6\u6570\u636e\u3002<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528 OFFSET \u548c LIMIT \u5b50\u53e5\u7ec4\u5408\u6765\u5b9e\u73b0\u590d\u6742\u7684\u5206\u9875\u9700\u6c42\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5206\u9875\u600e\u4e48\u5b9e\u73b0\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>\u5728 mysql \u4e2d\u5b9e\u73b0\u5206\u9875\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u786e\u5b9a\u6570\u636e\u8868\u3002\u6307\u5b9a\u6bcf\u9875\u884c\u6570\u3002\u8ba1\u7b97\u5f53\u524d\u9875\u7801\u3002\u4f7f\u7528 limit \u5b50\u53e5\u8bbe\u7f6e\u504f\u79fb\u91cf\u548c\u884c\u6570\uff0c\u4ece\u6307\u5b9a\u7684\u884c\u5f00\u59cb\u83b7\u53d6\u6570\u636e\u3002 MySQL \u5206\u9875\u5b9e\u73b0 \u5982\u4f55\u5b9e\u73b0 MySQL \u5206\u9875\uff1f \u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\u5206\u9875\uff1a 1. \u786e\u5b9a\u8981\u5206\u9875\u7684\u6570\u636e\u8868\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a user \u7684\u8868\uff0c\u5176\u4e2d\u5b58\u50a8\u7740\u7528\u6237\u6570\u636e\u3002 2. \u786e\u5b9a\u6bcf\u9875\u8981\u663e\u793a\u7684\u884c\u6570\u4f8b\u5982\uff0c\u6bcf\u9875\u663e\u793a 20 \u884c\u3002 3. \u786e\u5b9a\u5f53\u524d\u9875\u7801\u4f8b\u5982\uff0c\u7528\u6237\u6b63\u5728\u67e5\u770b\u7b2c 2 \u9875\u3002 4. \u4f7f\u7528 LIMIT \u5b50\u53e5 SELECT * FROM user LIMIT N, M; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a N \u662f\u504f\u79fb\u91cf\uff0c\u8868\u793a\u4ece\u7b2c N \u884c\u5f00\u59cb\u83b7\u53d6\u6570\u636e\u3002 M \u662f\u8981\u83b7\u53d6\u7684\u884c\u6570\u3002 5. \u8ba1\u7b97\u504f\u79fb\u91cf\u504f\u79fb\u91cf\u662f\u6bcf\u9875\u884c\u6570\u4e0e\uff08\u5f53\u524d\u9875\u7801 &#8211; 1\uff09\u7684\u4e58\u79ef\u3002 OFFSET = (Page Number &#8211; 1) * [&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-6327","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6327","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=6327"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6327\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}