{"id":6653,"date":"2024-11-14T09:21:10","date_gmt":"2024-11-14T01:21:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6653\/"},"modified":"2024-11-14T09:21:10","modified_gmt":"2024-11-14T01:21:10","slug":"group-by%e5%8f%af%e4%bb%a5%e6%9c%89%e4%b8%a4%e4%b8%aa%e6%9d%a1%e4%bb%b6%e5%90%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6653\/","title":{"rendered":"group by\u53ef\u4ee5\u6709\u4e24\u4e2a\u6761\u4ef6\u5417"},"content":{"rendered":"<blockquote><p>\n  \u662f\u7684\uff0cgroup by \u53ef\u4ee5\u6709\u4e24\u4e2a\u6761\u4ef6\u3002\u4f7f\u7528 group by \u8bed\u53e5\u65f6\uff0c\u53ef\u4ee5\u5728 group by \u5b50\u53e5\u4e2d\u6307\u5b9a\u591a\u4e2a\u5217\u540d\uff0c\u4ee5\u4fbf\u6309\u8fd9\u4e9b\u5217\u7684\u503c\u5206\u7ec4\u6570\u636e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202409\/11\/2024091107340615117.jpg\" class=\"aligncenter\" title=\"group by\u53ef\u4ee5\u6709\u4e24\u4e2a\u6761\u4ef6\u5417\u63d2\u56fe\" alt=\"group by\u53ef\u4ee5\u6709\u4e24\u4e2a\u6761\u4ef6\u5417\u63d2\u56fe\" \/><\/p>\n<p><strong>group by \u53ef\u4ee5\u6709\u4e24\u4e2a\u6761\u4ef6\u5417\uff1f<\/strong><\/p>\n<p>\u662f\u7684\uff0cgroup by \u53ef\u4ee5\u6709\u4e24\u4e2a\u6761\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 group by \u6765\u5206\u7ec4\u591a\u4e2a\u5217\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528 group by \u8bed\u53e5\u65f6\uff0c\u53ef\u4ee5\u5728 GROUP BY \u5b50\u53e5\u4e2d\u6307\u5b9a\u591a\u4e2a\u5217\u540d\uff0c\u5c06\u6570\u636e\u6309\u8fd9\u4e9b\u5217\u7684\u503c\u8fdb\u884c\u5206\u7ec4\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT column1, column2, ...\nFROM table_name\nGROUP BY column1, column2, ...<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u67e5\u8be2\u6309 country \u548c city \u5bf9 customers \u8868\u8fdb\u884c\u5206\u7ec4\uff1a<\/p>\n<pre>SELECT country, city, SUM(sales)\nFROM customers\nGROUP BY country, city<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<p>\u8be5\u67e5\u8be2\u5c06\u8fd4\u56de\u4e00\u884c\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u4e2a\u552f\u4e00\u56fd\u5bb6\/\u57ce\u5e02\u7ec4\u5408\u7684\u9500\u552e\u603b\u548c\u3002<\/p>\n<p><strong>\u4f7f\u7528 group by \u65f6\u7684\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728 GROUP BY \u5b50\u53e5\u4e2d\u6307\u5b9a\u7684\u5217\u5fc5\u987b\u5728 SELECT \u5b50\u53e5\u4e2d\u6307\u5b9a\u3002<\/li>\n<li>\u5982\u679c\u5728 GROUP BY \u5b50\u53e5\u4e2d\u6307\u5b9a\u4e86\u591a\u4e2a\u5217\uff0c\u5219 SELECT \u5b50\u53e5\u4e2d\u53ea\u80fd\u4f7f\u7528\uff08\u5982 SUM()\u3001COUNT()\u3001AVG() \u7b49\uff09\u3002<\/li>\n<li>\u5bf9\u4e8e\u6bcf\u4e2a\u5206\u7ec4\uff0c\u805a\u5408\u51fd\u6570\u5c06\u5bf9\u540c\u4e00\u7ec4\u4e2d\u7684\u6240\u6709\u884c\u4e0a\u7684\u6307\u5b9a\u5217\u7684\u503c\u8fdb\u884c\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fgroup by\u53ef\u4ee5\u6709\u4e24\u4e2a\u6761\u4ef6\u5417\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>\u662f\u7684\uff0cgroup by \u53ef\u4ee5\u6709\u4e24\u4e2a\u6761\u4ef6\u3002\u4f7f\u7528 group by \u8bed\u53e5\u65f6\uff0c\u53ef\u4ee5\u5728 group by \u5b50\u53e5\u4e2d\u6307\u5b9a\u591a\u4e2a\u5217\u540d\uff0c\u4ee5\u4fbf\u6309\u8fd9\u4e9b\u5217\u7684\u503c\u5206\u7ec4\u6570\u636e\u3002 group by \u53ef\u4ee5\u6709\u4e24\u4e2a\u6761\u4ef6\u5417\uff1f \u662f\u7684\uff0cgroup by \u53ef\u4ee5\u6709\u4e24\u4e2a\u6761\u4ef6\u3002 \u5982\u4f55\u4f7f\u7528 group by \u6765\u5206\u7ec4\u591a\u4e2a\u5217\uff1f \u4f7f\u7528 group by \u8bed\u53e5\u65f6\uff0c\u53ef\u4ee5\u5728 GROUP BY \u5b50\u53e5\u4e2d\u6307\u5b9a\u591a\u4e2a\u5217\u540d\uff0c\u5c06\u6570\u636e\u6309\u8fd9\u4e9b\u5217\u7684\u503c\u8fdb\u884c\u5206\u7ec4\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a SELECT column1, column2, &#8230; FROM table_name GROUP BY column1, column2, &#8230; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a \u4ee5\u4e0b\u67e5\u8be2\u6309 country \u548c city \u5bf9 customers \u8868\u8fdb\u884c\u5206\u7ec4\uff1a SELECT country, city, SUM(sales) FROM customers GROUP BY country, [&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-6653","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6653","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=6653"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6653\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}