{"id":6652,"date":"2024-11-14T14:16:58","date_gmt":"2024-11-14T06:16:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6652\/"},"modified":"2024-11-14T14:16:58","modified_gmt":"2024-11-14T06:16:58","slug":"group-by%e5%87%bd%e6%95%b0%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6652\/","title":{"rendered":"group by\u51fd\u6570\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  group by \u51fd\u6570\u53ef\u5c06\u6570\u636e\u6309\u5217\u503c\u5206\u7ec4\uff0c\u5e94\u7528\u805a\u5408\u51fd\u6570\uff08\u5982 sum\uff09\u8fd0\u7b97\uff0c\u4ee5\u663e\u793a\u6bcf\u4e2a\u7ec4\u7684\u552f\u4e00\u503c\u548c\u805a\u5408\u7ed3\u679c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202409\/11\/2024091107310274087.jpg\" class=\"aligncenter\" title=\"group by\u51fd\u6570\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"group by\u51fd\u6570\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>GROUP BY \u51fd\u6570<\/strong><\/p>\n<p>GROUP BY \u51fd\u6570\u5141\u8bb8\u60a8\u5c06\u6570\u636e\u96c6\u4e2d\u7684\u884c\u5206\u7ec4\uff0c\u57fa\u4e8e\u4e00\u4e2a\u6216\u591a\u4e2a\u5171\u540c\u7684\u5217\u503c\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>SELECT \u5217\u540d, \u805a\u5408\u51fd\u6570(\u5217\u540d)\nFROM \u8868\u540d\nGROUP BY \u5217\u540d<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u53c2\u6570<\/strong><\/p>\n<ul>\n<li> <strong>\u5217\u540d\uff1a<\/strong>\u8981\u5206\u7ec4\u7684\u5217\u3002<\/li>\n<li> <strong>\uff1a<\/strong>\u5728\u6bcf\u4e2a\u7ec4\u5185\u5e94\u7528\u7684\u805a\u5408\u51fd\u6570\uff0c\u5982 SUM\u3001COUNT\u3001AVG \u7b49\u3002<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<ul>\n<li>\u5c06\u6570\u636e\u96c6\u5206\u7ec4\u540e\uff0c\u805a\u5408\u51fd\u6570\u5c06\u5bf9\u6bcf\u4e2a\u7ec4\u4e2d\u7684\u503c\u8fdb\u884c\u8fd0\u7b97\u3002<\/li>\n<li>\u7ed3\u679c\u96c6\u4e2d\uff0c\u5c06\u663e\u793a\u6bcf\u4e2a\u7ec4\u7684\u552f\u4e00\u503c\u4ee5\u53ca\u805a\u5408\u51fd\u6570\u7684\u8ba1\u7b97\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u5982\u4e0b\u6570\u636e\u96c6\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u8ba2\u5355\u53f7<\/th>\n<th>\u4ea7\u54c1<\/th>\n<th>\u5355\u4ef7<\/th>\n<th>\u6570\u91cf<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>\u4ea7\u54c1 A<\/td>\n<td>10<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\u4ea7\u54c1 B<\/td>\n<td>12<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>\u4ea7\u54c1 A<\/td>\n<td>15<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>\u4ea7\u54c1 B<\/td>\n<td>18<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>\u4ea7\u54c1 C<\/td>\n<td>20<\/td>\n<td>4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u82e5\u8981\u8ba1\u7b97\u6bcf\u79cd\u4ea7\u54c1\u7684\u603b\u9500\u552e\u989d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b GROUP BY \u67e5\u8be2\uff1a<\/p>\n<pre>SELECT \u4ea7\u54c1, SUM(\u6570\u91cf * \u5355\u4ef7) AS \u603b\u9500\u552e\u989d\nFROM \u9500\u552e\u6570\u636e\nGROUP BY \u4ea7\u54c1<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\u4ea7\u54c1<\/th>\n<th>\u603b\u9500\u552e\u989d<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u4ea7\u54c1 A<\/td>\n<td>130<\/td>\n<\/tr>\n<tr>\n<td>\u4ea7\u54c1 B<\/td>\n<td>42<\/td>\n<\/tr>\n<tr>\n<td>\u4ea7\u54c1 C<\/td>\n<td>80<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ee5\u4e0a\u5c31\u662fgroup by\u51fd\u6570\u600e\u4e48\u7528\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>group by \u51fd\u6570\u53ef\u5c06\u6570\u636e\u6309\u5217\u503c\u5206\u7ec4\uff0c\u5e94\u7528\u805a\u5408\u51fd\u6570\uff08\u5982 sum\uff09\u8fd0\u7b97\uff0c\u4ee5\u663e\u793a\u6bcf\u4e2a\u7ec4\u7684\u552f\u4e00\u503c\u548c\u805a\u5408\u7ed3\u679c\u3002 GROUP BY \u51fd\u6570 GROUP BY \u51fd\u6570\u5141\u8bb8\u60a8\u5c06\u6570\u636e\u96c6\u4e2d\u7684\u884c\u5206\u7ec4\uff0c\u57fa\u4e8e\u4e00\u4e2a\u6216\u591a\u4e2a\u5171\u540c\u7684\u5217\u503c\u3002 \u8bed\u6cd5 SELECT \u5217\u540d, \u805a\u5408\u51fd\u6570(\u5217\u540d) FROM \u8868\u540d GROUP BY \u5217\u540d \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570 \u5217\u540d\uff1a\u8981\u5206\u7ec4\u7684\u5217\u3002 \uff1a\u5728\u6bcf\u4e2a\u7ec4\u5185\u5e94\u7528\u7684\u805a\u5408\u51fd\u6570\uff0c\u5982 SUM\u3001COUNT\u3001AVG \u7b49\u3002 \u7528\u6cd5 \u5c06\u6570\u636e\u96c6\u5206\u7ec4\u540e\uff0c\u805a\u5408\u51fd\u6570\u5c06\u5bf9\u6bcf\u4e2a\u7ec4\u4e2d\u7684\u503c\u8fdb\u884c\u8fd0\u7b97\u3002 \u7ed3\u679c\u96c6\u4e2d\uff0c\u5c06\u663e\u793a\u6bcf\u4e2a\u7ec4\u7684\u552f\u4e00\u503c\u4ee5\u53ca\u805a\u5408\u51fd\u6570\u7684\u8ba1\u7b97\u7ed3\u679c\u3002 \u793a\u4f8b \u8003\u8651\u5982\u4e0b\u6570\u636e\u96c6\uff1a \u8ba2\u5355\u53f7 \u4ea7\u54c1 \u5355\u4ef7 \u6570\u91cf 1 \u4ea7\u54c1 A 10 5 2 \u4ea7\u54c1 B 12 3 3 \u4ea7\u54c1 A 15 2 4 \u4ea7\u54c1 B 18 1 5 [&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-6652","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6652","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=6652"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6652\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}