{"id":6201,"date":"2024-11-14T11:32:50","date_gmt":"2024-11-14T03:32:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6201\/"},"modified":"2024-11-14T11:32:50","modified_gmt":"2024-11-14T03:32:50","slug":"mysql%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8groupby","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6201\/","title":{"rendered":"mysql\u5982\u4f55\u4f7f\u7528groupby"},"content":{"rendered":"<blockquote><p>\n  mysql \u7684 group by \u5b50\u53e5\u7528\u4e8e\u5c06\u6570\u636e\u5206\u7ec4\u5e76\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\uff0c\u8bed\u6cd5\uff1aselect aggregate_function(column_name) from table_name group by column_name\u3002\u5b83\u5141\u8bb8\u6839\u636e\u6307\u5b9a\u5217\u5bf9\u6570\u636e\u5206\u7ec4\uff0c\u7136\u540e\u5bf9\u6bcf\u4e00\u7ec4\u8fdb\u884c\u805a\u5408\u8ba1\u7b97\uff0c\u4f8b\u5982\u6c42\u548c\u6216\u6c42\u5e73\u5747\u503c\u3002\u901a\u8fc7\u4f7f\u7528 group by\uff0c\u53ef\u4ee5\u5206\u6790\u548c\u603b\u7ed3\u590d\u6742\u6570\u636e\u96c6\u4e2d\u7684\u6570\u636e\uff0c\u63d0\u53d6\u6709\u610f\u4e49\u7684\u89c1\u89e3\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/15\/2024061512572880003.jpg\" class=\"aligncenter\" title=\"mysql\u5982\u4f55\u4f7f\u7528groupby\u63d2\u56fe\" alt=\"mysql\u5982\u4f55\u4f7f\u7528groupby\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u7684 GROUP BY<\/strong><\/p>\n<p>GROUP BY \u5b50\u53e5\u7528\u4e8e\u5c06\u6570\u636e\u5206\u7ec4\uff0c\u4ee5\u4fbf\u5bf9\u540c\u4e00\u7ec4\u5185\u7684\u503c\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\uff08\u4f8b\u5982\u6c42\u548c\u3001\u6c42\u5e73\u5747\u503c\u3001\u8ba1\u6570\uff09\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT aggregate_function(column_name)\nFROM table_name\nGROUP BY column_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u53c2\u6570\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>aggregate_function()\uff1a<\/strong>\u8981\u6267\u884c\u7684\uff0c\u4f8b\u5982 SUM()\u3001AVG()\u3001COUNT()\u3002<\/li>\n<li> <strong>column_name\uff1a<\/strong>\u8981\u5206\u7ec4\u7684\u5217\u3002<\/li>\n<\/ul>\n<p><strong>\u5982\u4f55\u4f7f\u7528\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u786e\u5b9a\u8981\u5206\u7ec4\u7684\u5217\uff1a<\/strong>\u9009\u62e9\u5305\u542b\u8981\u5206\u7ec4\u6570\u636e\u7684\u503c\u7684\u5217\u3002<\/li>\n<li> <strong>\u9009\u62e9\u805a\u5408\u51fd\u6570\uff1a<\/strong>\u786e\u5b9a\u8981\u5bf9\u5206\u7ec4\u503c\u6267\u884c\u7684\u805a\u5408\u64cd\u4f5c\u3002<\/li>\n<li> <strong>\u7f16\u5199 GROUP BY \u5b50\u53e5\uff1a<\/strong>\u5728 SELECT \u8bed\u53e5\u7684\u7ed3\u5c3e\u6dfb\u52a0 GROUP BY \u5b50\u53e5\uff0c\u540e\u8ddf\u8981\u5206\u7ec4\u7684\u5217\u540d\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u540d\u4e3a &#8220;orders&#8221; \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u5217\uff1a<\/p>\n<ul>\n<li>customer_id<\/li>\n<li>product_id<\/li>\n<li>quantity<\/li>\n<\/ul>\n<p>\u8981\u7edf\u8ba1\u6bcf\u4e2a\u5ba2\u6237\u8ba2\u8d2d\u7684\u5546\u54c1\u6570\u91cf\u4e4b\u548c\uff0c\u53ef\u4ee5\u7f16\u5199\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT customer_id, SUM(quantity) AS total_quantity\nFROM orders\nGROUP BY customer_id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>customer_id | total_quantity\n------------+----------------\n1           | 5\n2           | 10\n3           | 3<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 GROUP BY\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5206\u6790\u548c\u603b\u7ed3\u590d\u6742\u6570\u636e\u96c6\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528groupby\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 \u7684 group by \u5b50\u53e5\u7528\u4e8e\u5c06\u6570\u636e\u5206\u7ec4\u5e76\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\uff0c\u8bed\u6cd5\uff1aselect aggregate_function(column_name) from table_name group by column_name\u3002\u5b83\u5141\u8bb8\u6839\u636e\u6307\u5b9a\u5217\u5bf9\u6570\u636e\u5206\u7ec4\uff0c\u7136\u540e\u5bf9\u6bcf\u4e00\u7ec4\u8fdb\u884c\u805a\u5408\u8ba1\u7b97\uff0c\u4f8b\u5982\u6c42\u548c\u6216\u6c42\u5e73\u5747\u503c\u3002\u901a\u8fc7\u4f7f\u7528 group by\uff0c\u53ef\u4ee5\u5206\u6790\u548c\u603b\u7ed3\u590d\u6742\u6570\u636e\u96c6\u4e2d\u7684\u6570\u636e\uff0c\u63d0\u53d6\u6709\u610f\u4e49\u7684\u89c1\u89e3\u3002 \u5982\u4f55\u4f7f\u7528 MySQL \u7684 GROUP BY GROUP BY \u5b50\u53e5\u7528\u4e8e\u5c06\u6570\u636e\u5206\u7ec4\uff0c\u4ee5\u4fbf\u5bf9\u540c\u4e00\u7ec4\u5185\u7684\u503c\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\uff08\u4f8b\u5982\u6c42\u548c\u3001\u6c42\u5e73\u5747\u503c\u3001\u8ba1\u6570\uff09\u3002 \u8bed\u6cd5\uff1a SELECT aggregate_function(column_name) FROM table_name GROUP BY column_name; \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570\uff1a aggregate_function()\uff1a\u8981\u6267\u884c\u7684\uff0c\u4f8b\u5982 SUM()\u3001AVG()\u3001COUNT()\u3002 column_name\uff1a\u8981\u5206\u7ec4\u7684\u5217\u3002 \u5982\u4f55\u4f7f\u7528\uff1a \u786e\u5b9a\u8981\u5206\u7ec4\u7684\u5217\uff1a\u9009\u62e9\u5305\u542b\u8981\u5206\u7ec4\u6570\u636e\u7684\u503c\u7684\u5217\u3002 \u9009\u62e9\u805a\u5408\u51fd\u6570\uff1a\u786e\u5b9a\u8981\u5bf9\u5206\u7ec4\u503c\u6267\u884c\u7684\u805a\u5408\u64cd\u4f5c\u3002 \u7f16\u5199 GROUP BY \u5b50\u53e5\uff1a\u5728 SELECT \u8bed\u53e5\u7684\u7ed3\u5c3e\u6dfb\u52a0 GROUP BY \u5b50\u53e5\uff0c\u540e\u8ddf\u8981\u5206\u7ec4\u7684\u5217\u540d\u3002 \u793a\u4f8b\uff1a \u8003\u8651\u4e00\u4e2a\u540d\u4e3a &#8220;orders&#8221; \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u5217\uff1a customer_id product_id quantity \u8981\u7edf\u8ba1\u6bcf\u4e2a\u5ba2\u6237\u8ba2\u8d2d\u7684\u5546\u54c1\u6570\u91cf\u4e4b\u548c\uff0c\u53ef\u4ee5\u7f16\u5199\u4ee5\u4e0b\u67e5\u8be2\uff1a SELECT [&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-6201","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6201","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=6201"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6201\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}