{"id":6328,"date":"2024-11-14T08:45:07","date_gmt":"2024-11-14T00:45:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6328\/"},"modified":"2024-11-14T08:45:07","modified_gmt":"2024-11-14T00:45:07","slug":"mysql%e5%88%86%e9%a1%b5%e7%9a%84%e5%87%a0%e7%a7%8d%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6328\/","title":{"rendered":"mysql\u5206\u9875\u7684\u51e0\u79cd\u65b9\u5f0f"},"content":{"rendered":"<blockquote><p>\n  mysql \u5206\u9875\u6709\u56db\u79cd\u65b9\u6cd5\uff1a1. limit \u5b50\u53e5\uff08\u6307\u5b9a\u8d77\u59cb\u884c\u548c\u884c\u6570\uff09\uff1b2. offset \u5b50\u53e5\uff08\u6307\u5b9a\u884c\u504f\u79fb\u91cf\u548c\u884c\u6570\uff09\uff1b3. \u5b50\u67e5\u8be2\uff08\u4f7f\u7528\u4e00\u4e2a\u67e5\u8be2\u7b5b\u9009\u53e6\u4e00\u4e2a\u67e5\u8be2\uff09\uff1b4. \u6e38\u6807\uff08\u9010\u884c\u904d\u5386\u6570\u636e\u96c6\uff09\u3002\u9009\u62e9\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u96c6\u5927\u5c0f\u3001\u7b5b\u9009\u6761\u4ef6\u548c\u5904\u7406\u9700\u8981\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202407\/31\/2024073122455034043.jpg\" class=\"aligncenter\" title=\"mysql\u5206\u9875\u7684\u51e0\u79cd\u65b9\u5f0f\u63d2\u56fe\" alt=\"mysql\u5206\u9875\u7684\u51e0\u79cd\u65b9\u5f0f\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u5206\u9875\u7684\u51e0\u79cd\u65b9\u5f0f<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\uff0c\u5206\u9875\u662f\u4e00\u79cd\u7528\u4e8e\u68c0\u7d22\u5927\u6570\u636e\u96c6\u7684\u7279\u5b9a\u90e8\u5206\u7684\u6280\u672f\u3002\u5f53\u6570\u636e\u96c6\u592a\u5927\u65f6\uff0c\u4e00\u6b21\u6027\u68c0\u7d22\u6240\u6709\u6570\u636e\u4f1a\u5f88\u8017\u8d39\u8d44\u6e90\u3002\u5206\u9875\u5141\u8bb8\u6211\u4eec\u4e00\u6b21\u68c0\u7d22\u6570\u636e\u96c6\u7684\u4e00\u90e8\u5206\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p><strong>1. LIMIT \u5b50\u53e5<\/strong><\/p>\n<p>LIMIT \u5b50\u53e5\u662f\u6700\u7b80\u5355\u4e5f\u662f\u6700\u5e38\u7528\u7684\u5206\u9875\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8\u60a8\u6307\u5b9a\u8981\u68c0\u7d22\u7684\u884c\u7684\u6570\u91cf\u4ee5\u53ca\u8981\u4ece\u4f55\u5904\u5f00\u59cb\u68c0\u7d22\u3002<\/p>\n<pre>SELECT * FROM table_name LIMIT start, count;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>start\uff1a\u6307\u5b9a\u4ece\u6570\u636e\u96c6\u4e2d\u7684\u7b2c\u51e0\u884c\u5f00\u59cb\u68c0\u7d22\u3002<\/li>\n<li>count\uff1a\u6307\u5b9a\u8981\u68c0\u7d22\u7684\u884c\u6570\u3002<\/li>\n<\/ul>\n<p><strong>2. OFFSET \u5b50\u53e5<\/strong><\/p>\n<p>OFFSET \u5b50\u53e5\u4e0e LIMIT \u5b50\u53e5\u7c7b\u4f3c\uff0c\u4f46\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u6307\u5b9a\u884c\u504f\u79fb\u91cf\u540e\u68c0\u7d22\u884c\u3002<\/p>\n<pre>SELECT * FROM table_name ORDER BY id OFFSET start ROWS FETCH NEXT count ROWS ONLY;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>start\uff1a\u6307\u5b9a\u4ece\u6570\u636e\u96c6\u4e2d\u7684\u7b2c\u51e0\u884c\u5f00\u59cb\u68c0\u7d22\u3002<\/li>\n<li>count\uff1a\u6307\u5b9a\u8981\u68c0\u7d22\u7684\u884c\u6570\u3002<\/li>\n<\/ul>\n<p><strong>3. \u5b50\u67e5\u8be2<\/strong><\/p>\n<p>\u5b50\u67e5\u8be2\u662f\u4e00\u79cd\u66f4\u9ad8\u7ea7\u7684\u5206\u9875\u65b9\u6cd5\uff0c\u6d89\u53ca\u4f7f\u7528\u4e00\u4e2a\u67e5\u8be2\u6765\u8fc7\u6ee4\u53e6\u4e00\u4e2a\u67e5\u8be2\u3002<\/p>\n<pre>SELECT * FROM table_name WHERE id IN (\n    SELECT id FROM (\n        SELECT id\n        FROM table_name\n        ORDER BY id\n        LIMIT start, count\n    ) AS subquery\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>start\uff1a\u6307\u5b9a\u4ece\u6570\u636e\u96c6\u4e2d\u7684\u7b2c\u51e0\u884c\u5f00\u59cb\u68c0\u7d22\u3002<\/li>\n<li>count\uff1a\u6307\u5b9a\u8981\u68c0\u7d22\u7684\u884c\u6570\u3002<\/li>\n<\/ul>\n<p><strong>4. \u6e38\u6807<\/strong><\/p>\n<p>\u6e38\u6807\u662f MySQL \u4e2d\u7528\u4e8e\u9010\u884c\u904d\u5386\u6570\u636e\u96c6\u7684\u673a\u5236\u3002\u53ef\u4ee5\u5c06\u6e38\u6807\u7528\u4e8e\u5206\u9875\uff0c\u56e0\u4e3a\u60a8\u53ef\u4ee5\u5c06\u6e38\u6807\u5b9a\u4f4d\u5230\u6570\u636e\u96c6\u4e2d\u7684\u7279\u5b9a\u884c\u5e76\u9010\u884c\u68c0\u7d22\u3002<\/p>\n<pre>DECLARE cursor_name CURSOR FOR SELECT * FROM table_name;\nOPEN cursor_name;\nFETCH count ROWS ONLY FROM cursor_name INTO @row1, @row2, ...;\nCLOSE cursor_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>count\uff1a\u6307\u5b9a\u8981\u68c0\u7d22\u7684\u884c\u6570\u3002<\/li>\n<\/ul>\n<p><strong>\u9009\u62e9\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u5206\u9875\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u51c6\u5219\uff1a<\/p>\n<ul>\n<li> <strong>LIMIT \u548c OFFSET \u5b50\u53e5<\/strong>\uff1a\u9002\u7528\u4e8e\u5c0f\u6570\u636e\u96c6\u6216\u9700\u8981\u5feb\u901f\u7ed3\u679c\u7684\u60c5\u51b5\u3002<\/li>\n<li> <strong>\u5b50\u67e5\u8be2<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u6570\u636e\u96c6\u6216\u9700\u8981\u590d\u6742\u7b5b\u9009\u6761\u4ef6\u7684\u60c5\u51b5\u3002<\/li>\n<li> <strong>\u6e38\u6807<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u9010\u884c\u904d\u5386\u6570\u636e\u96c6\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5206\u9875\u7684\u51e0\u79cd\u65b9\u5f0f\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>mysql \u5206\u9875\u6709\u56db\u79cd\u65b9\u6cd5\uff1a1. limit \u5b50\u53e5\uff08\u6307\u5b9a\u8d77\u59cb\u884c\u548c\u884c\u6570\uff09\uff1b2. offset \u5b50\u53e5\uff08\u6307\u5b9a\u884c\u504f\u79fb\u91cf\u548c\u884c\u6570\uff09\uff1b3. \u5b50\u67e5\u8be2\uff08\u4f7f\u7528\u4e00\u4e2a\u67e5\u8be2\u7b5b\u9009\u53e6\u4e00\u4e2a\u67e5\u8be2\uff09\uff1b4. \u6e38\u6807\uff08\u9010\u884c\u904d\u5386\u6570\u636e\u96c6\uff09\u3002\u9009\u62e9\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u96c6\u5927\u5c0f\u3001\u7b5b\u9009\u6761\u4ef6\u548c\u5904\u7406\u9700\u8981\u3002 MySQL \u5206\u9875\u7684\u51e0\u79cd\u65b9\u5f0f \u5728 MySQL \u4e2d\uff0c\u5206\u9875\u662f\u4e00\u79cd\u7528\u4e8e\u68c0\u7d22\u5927\u6570\u636e\u96c6\u7684\u7279\u5b9a\u90e8\u5206\u7684\u6280\u672f\u3002\u5f53\u6570\u636e\u96c6\u592a\u5927\u65f6\uff0c\u4e00\u6b21\u6027\u68c0\u7d22\u6240\u6709\u6570\u636e\u4f1a\u5f88\u8017\u8d39\u8d44\u6e90\u3002\u5206\u9875\u5141\u8bb8\u6211\u4eec\u4e00\u6b21\u68c0\u7d22\u6570\u636e\u96c6\u7684\u4e00\u90e8\u5206\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002 1. LIMIT \u5b50\u53e5 LIMIT \u5b50\u53e5\u662f\u6700\u7b80\u5355\u4e5f\u662f\u6700\u5e38\u7528\u7684\u5206\u9875\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8\u60a8\u6307\u5b9a\u8981\u68c0\u7d22\u7684\u884c\u7684\u6570\u91cf\u4ee5\u53ca\u8981\u4ece\u4f55\u5904\u5f00\u59cb\u68c0\u7d22\u3002 SELECT * FROM table_name LIMIT start, count; \u767b\u5f55\u540e\u590d\u5236 start\uff1a\u6307\u5b9a\u4ece\u6570\u636e\u96c6\u4e2d\u7684\u7b2c\u51e0\u884c\u5f00\u59cb\u68c0\u7d22\u3002 count\uff1a\u6307\u5b9a\u8981\u68c0\u7d22\u7684\u884c\u6570\u3002 2. OFFSET \u5b50\u53e5 OFFSET \u5b50\u53e5\u4e0e LIMIT \u5b50\u53e5\u7c7b\u4f3c\uff0c\u4f46\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u6307\u5b9a\u884c\u504f\u79fb\u91cf\u540e\u68c0\u7d22\u884c\u3002 SELECT * FROM table_name ORDER BY id OFFSET start ROWS FETCH NEXT count ROWS ONLY; \u767b\u5f55\u540e\u590d\u5236 start\uff1a\u6307\u5b9a\u4ece\u6570\u636e\u96c6\u4e2d\u7684\u7b2c\u51e0\u884c\u5f00\u59cb\u68c0\u7d22\u3002 count\uff1a\u6307\u5b9a\u8981\u68c0\u7d22\u7684\u884c\u6570\u3002 3. \u5b50\u67e5\u8be2 [&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-6328","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6328","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=6328"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6328\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}