{"id":6326,"date":"2024-11-14T09:31:36","date_gmt":"2024-11-14T01:31:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6326\/"},"modified":"2024-11-14T09:31:36","modified_gmt":"2024-11-14T01:31:36","slug":"mysql%e5%88%86%e9%a1%b5%e6%9f%a5%e8%af%a2%e8%8e%b7%e5%8f%96%e6%9d%a1%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6326\/","title":{"rendered":"mysql\u5206\u9875\u67e5\u8be2\u83b7\u53d6\u6761\u6570"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\u83b7\u53d6\u5206\u9875\u67e5\u8be2\u4e2d\u7684\u6761\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528 count() \u51fd\u6570\u8ba1\u7b97\u7ed3\u679c\u96c6\u4e2d\u7684\u884c\u6570\uff0c\u7ed3\u5408 limit \u5b50\u53e5\u9650\u5236\u8fd4\u56de\u7684\u884c\u6570\uff0c\u4ee5\u53ea\u83b7\u53d6\u5f53\u524d\u9875\u7684\u6761\u6570\u3002\u8fd9\u6709\u52a9\u4e8e\u51c6\u786e\u4e86\u89e3\u7ed3\u679c\u96c6\u5305\u542b\u7684\u884c\u6570\uff0c\u5e76\u786e\u5b9a\u5206\u9875\u7684\u603b\u9875\u6570\u4ee5\u53ca\u663e\u793a\u9002\u5f53\u7684\u5206\u9875\u63a7\u4ef6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202407\/31\/2024073122424924159.jpg\" class=\"aligncenter\" title=\"mysql\u5206\u9875\u67e5\u8be2\u83b7\u53d6\u6761\u6570\u63d2\u56fe\" alt=\"mysql\u5206\u9875\u67e5\u8be2\u83b7\u53d6\u6761\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u5206\u9875\u67e5\u8be2\u83b7\u53d6\u6761\u6570<\/strong><\/p>\n<p>\u5728 MySQL \u6570\u636e\u5e93\u4e2d\uff0c\u5206\u9875\u67e5\u8be2\u662f\u83b7\u53d6\u5927\u6570\u636e\u96c6\u7279\u5b9a\u90e8\u5206\u7684\u4e00\u79cd\u6709\u6548\u65b9\u5f0f\u3002\u5206\u9875\u67e5\u8be2\u901a\u8fc7\u5c06\u7ed3\u679c\u96c6\u5206\u5272\u6210\u8f83\u5c0f\u7684\u5b50\u96c6\u6765\u5b9e\u73b0\uff0c\u4ece\u800c\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u5e76\u51cf\u5c11\u7f51\u7edc\u6d41\u91cf\u3002<\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6\u5206\u9875\u67e5\u8be2\u4e2d\u7684\u6761\u6570<\/strong><\/p>\n<p>\u83b7\u53d6\u5206\u9875\u67e5\u8be2\u4e2d\u7684\u6761\u6570\u9700\u8981\u4f7f\u7528 COUNT() \u51fd\u6570\u3002\u8be5\u51fd\u6570\u8ba1\u7b97\u7ed3\u679c\u96c6\u4e2d\u7684\u884c\u6570\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT COUNT(*) FROM table_name\nWHERE condition;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a users \u7684\u8868\uff0c\u8981\u83b7\u53d6\u6240\u6709\u7528\u6237\u7684\u603b\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT COUNT(*) FROM users;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong> COUNT(*) \u51fd\u6570\u4f1a\u8ba1\u7b97\u8868\u4e2d\u7684\u6240\u6709\u884c\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u5f53\u524d\u9875\u4e2d\u7684\u884c\u3002<\/p>\n<p><strong>\u9650\u5236\u67e5\u8be2\u7ed3\u679c<\/strong><\/p>\n<p>\u4e3a\u4e86\u53ea\u83b7\u53d6\u5f53\u524d\u9875\u7684\u6761\u6570\uff0c\u53ef\u4ee5\u5c06 COUNT() \u51fd\u6570\u4e0e LIMIT \u5b50\u53e5\u7ed3\u5408\u4f7f\u7528\u3002LIMIT \u5b50\u53e5\u9650\u5236\u8fd4\u56de\u7684\u884c\u6570\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT COUNT(*) FROM table_name\nWHERE condition\nLIMIT offset, limit;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u60f3\u83b7\u53d6\u7b2c 2 \u9875\uff08\u6bcf\u9875 10 \u884c\uff09\u4e2d\u7528\u6237\u7684\u603b\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT COUNT(*) FROM users\nLIMIT 10, 10;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0coffset \u4e3a 10\uff08\u7b2c 2 \u9875\u7684\u7b2c\u4e00\u4e2a\u884c\u7684\u7d22\u5f15\uff09\uff0climit \u4e3a 10\uff08\u6bcf\u9875\u7684\u884c\u6570\uff09\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u901a\u8fc7\u83b7\u53d6\u5206\u9875\u67e5\u8be2\u4e2d\u7684\u6761\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u51c6\u786e\u77e5\u9053\u7ed3\u679c\u96c6\u5305\u542b\u591a\u5c11\u884c\u3002<\/li>\n<li>\u8fd9\u5bf9\u4e8e\u786e\u5b9a\u5206\u9875\u7684\u603b\u9875\u6570\u4ee5\u53ca\u5728\u7528\u6237\u754c\u9762\u4e2d\u663e\u793a\u9002\u5f53\u7684\u5206\u9875\u63a7\u4ef6\u975e\u5e38\u6709\u7528\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5206\u9875\u67e5\u8be2\u83b7\u53d6\u6761\u6570\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\u83b7\u53d6\u5206\u9875\u67e5\u8be2\u4e2d\u7684\u6761\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528 count() \u51fd\u6570\u8ba1\u7b97\u7ed3\u679c\u96c6\u4e2d\u7684\u884c\u6570\uff0c\u7ed3\u5408 limit \u5b50\u53e5\u9650\u5236\u8fd4\u56de\u7684\u884c\u6570\uff0c\u4ee5\u53ea\u83b7\u53d6\u5f53\u524d\u9875\u7684\u6761\u6570\u3002\u8fd9\u6709\u52a9\u4e8e\u51c6\u786e\u4e86\u89e3\u7ed3\u679c\u96c6\u5305\u542b\u7684\u884c\u6570\uff0c\u5e76\u786e\u5b9a\u5206\u9875\u7684\u603b\u9875\u6570\u4ee5\u53ca\u663e\u793a\u9002\u5f53\u7684\u5206\u9875\u63a7\u4ef6\u3002 MySQL \u5206\u9875\u67e5\u8be2\u83b7\u53d6\u6761\u6570 \u5728 MySQL \u6570\u636e\u5e93\u4e2d\uff0c\u5206\u9875\u67e5\u8be2\u662f\u83b7\u53d6\u5927\u6570\u636e\u96c6\u7279\u5b9a\u90e8\u5206\u7684\u4e00\u79cd\u6709\u6548\u65b9\u5f0f\u3002\u5206\u9875\u67e5\u8be2\u901a\u8fc7\u5c06\u7ed3\u679c\u96c6\u5206\u5272\u6210\u8f83\u5c0f\u7684\u5b50\u96c6\u6765\u5b9e\u73b0\uff0c\u4ece\u800c\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u5e76\u51cf\u5c11\u7f51\u7edc\u6d41\u91cf\u3002 \u5982\u4f55\u83b7\u53d6\u5206\u9875\u67e5\u8be2\u4e2d\u7684\u6761\u6570 \u83b7\u53d6\u5206\u9875\u67e5\u8be2\u4e2d\u7684\u6761\u6570\u9700\u8981\u4f7f\u7528 COUNT() \u51fd\u6570\u3002\u8be5\u51fd\u6570\u8ba1\u7b97\u7ed3\u679c\u96c6\u4e2d\u7684\u884c\u6570\u3002 \u8bed\u6cd5\uff1a SELECT COUNT(*) FROM table_name WHERE condition; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a users \u7684\u8868\uff0c\u8981\u83b7\u53d6\u6240\u6709\u7528\u6237\u7684\u603b\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a SELECT COUNT(*) FROM users; \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\uff1a COUNT(*) \u51fd\u6570\u4f1a\u8ba1\u7b97\u8868\u4e2d\u7684\u6240\u6709\u884c\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u5f53\u524d\u9875\u4e2d\u7684\u884c\u3002 \u9650\u5236\u67e5\u8be2\u7ed3\u679c \u4e3a\u4e86\u53ea\u83b7\u53d6\u5f53\u524d\u9875\u7684\u6761\u6570\uff0c\u53ef\u4ee5\u5c06 COUNT() \u51fd\u6570\u4e0e LIMIT \u5b50\u53e5\u7ed3\u5408\u4f7f\u7528\u3002LIMIT \u5b50\u53e5\u9650\u5236\u8fd4\u56de\u7684\u884c\u6570\u3002 \u8bed\u6cd5\uff1a SELECT COUNT(*) FROM table_name WHERE condition LIMIT offset, limit; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a [&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-6326","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6326","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=6326"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6326\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}