{"id":46483,"date":"2024-12-01T08:37:36","date_gmt":"2024-12-01T00:37:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46483\/"},"modified":"2024-12-01T08:37:36","modified_gmt":"2024-12-01T00:37:36","slug":"%e5%a6%82%e4%bd%95%e5%b0%86-count-group-by-%e5%92%8c-select-%e8%af%ad%e5%8f%a5%e5%90%88%e5%b9%b6%e4%bb%a5%e5%ae%9e%e7%8e%b0%e6%95%b0%e6%8d%ae%e8%81%9a%e5%90%88%ef%bc%9f-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46483\/","title":{"rendered":"\u5982\u4f55\u5c06 COUNT GROUP BY \u548c SELECT \u8bed\u53e5\u5408\u5e76\u4ee5\u5b9e\u73b0\u6570\u636e\u805a\u5408\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5c06 COUNT GROUP BY \u548c SELECT \u8bed\u53e5\u5408\u5e76\u4ee5\u5b9e\u73b0\u6570\u636e\u805a\u5408\uff1f<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/17327023646746f09c47e82.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06 COUNT GROUP BY \u548c SELECT \u8bed\u53e5\u5408\u5e76\u4ee5\u5b9e\u73b0\u6570\u636e\u805a\u5408\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06 COUNT GROUP BY \u548c SELECT \u8bed\u53e5\u5408\u5e76\u4ee5\u5b9e\u73b0\u6570\u636e\u805a\u5408\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5408\u5e76 count group by \u548c select \u8bed\u53e5\u6267\u884c\u6570\u636e\u805a\u5408<\/strong><\/p>\n<p>\u5728\u5904\u7406\u6d89\u53ca\u6570\u636e\u805a\u5408\u7684\u67e5\u8be2\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u5c06 count group by \u548c select \u8bed\u53e5\u7ed3\u5408\u4f7f\u7528\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5c06\u4e24\u6761\u8bed\u53e5\u5408\u5e76\u4e3a\u4e00\u6761\uff0c\u4ee5\u9ad8\u6548\u5730\u83b7\u53d6\u6240\u9700\u6570\u636e\u3002<\/p>\n<p><strong>\u95ee\u9898\u5185\u5bb9\uff1a<\/strong><\/p>\n<p>\u4ee5 tb \u8868\u4e3a\u4f8b\uff0c\u5176\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>a<\/th>\n<th>b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u671f\u671b\u5f97\u5230\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>a<\/th>\n<th>b<\/th>\n<th>num<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>2<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>3<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b sql \u8bed\u53e5\u5c06 count group by \u548c select \u8bed\u53e5\u5408\u5e76\uff1a<\/p>\n<pre>select a, b, count(*) as count from test group by a, b<\/pre>\n<p>\u8be5\u8bed\u53e5\u5148\u4f7f\u7528 count(*) \u5bf9\u7ed3\u679c\u6309 a \u548c b \u5217\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u5728 select \u5b50\u53e5\u4e2d\u5c06\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u4e0e\u539f\u59cb\u8868\u4e2d\u7684 a \u548c b \u5217\u7ed3\u5408\u8d77\u6765\uff0c\u5f62\u6210\u6240\u9700\u7684\u6700\u7ec8\u7ed3\u679c\u3002<\/p>\n<p><strong>\u539f\u7406\uff1a<\/strong><\/p>\n<p>group by \u5b50\u53e5\u5c06\u6570\u636e\u6309\u6307\u5b9a\u5217\u5206\u7ec4\uff0c\u5e76\u5bf9\u6bcf\u4e00\u7ec4\u6267\u884c\u805a\u5408\u51fd\u6570\uff08\u5728\u672c\u4f8b\u4e2d\u4e3a count(*)\uff09\u3002group by \u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u805a\u5408\u8868\uff0c\u5176\u4e2d\u6bcf\u4e00\u884c\u4ee3\u8868\u4e00\u4e2a\u5206\u7ec4\uff0c\u5e76\u4e14\u805a\u5408\u51fd\u6570\u7684\u503c\u4f5c\u4e3a\u8be5\u5206\u7ec4\u7684\u5217\u663e\u793a\u3002<\/p>\n<p>\u5de6\u8fde\u63a5\uff08left join\uff09\u5c06\u539f\u59cb\u8868\u4e0e\u805a\u5408\u8868\u8fde\u63a5\u8d77\u6765\uff0c\u5176\u4e2d\u8fde\u63a5\u6761\u4ef6\u662f\u539f\u59cb\u8868\u4e2d\u7684 a \u548c b \u5217\u4e0e\u805a\u5408\u8868\u4e2d\u7684 a \u548c b \u5217\u76f8\u7b49\u3002\u8fde\u63a5\u540e\uff0c\u805a\u5408\u8868\u4e2d\u7684 count \u5217\u53ef\u4ee5\u4f5c\u4e3a\u539f\u59cb\u8868\u4e2d\u7684\u4e00\u4e2a\u9644\u52a0\u5217\u4f7f\u7528\u3002<\/p>\n<p>\u901a\u8fc7\u5c06 count group by \u548c select \u8bed\u53e5\u5408\u5e76\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u805a\u5408\u548c\u9009\u62e9\u64cd\u4f5c\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u591a\u4e2a\u67e5\u8be2\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u5c06 COUNT GROUP BY \u548c SELECT \u8bed\u53e5\u5408\u5e76\u4ee5\u5b9e\u73b0\u6570\u636e\u805a\u5408\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06 COUNT GROUP BY \u548c SELECT \u8bed\u53e5\u5408\u5e76\u4ee5\u5b9e\u73b0\u6570\u636e\u805a\u5408\uff1f \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01 \u5408\u5e76 count group by \u548c select \u8bed\u53e5\u6267\u884c\u6570\u636e\u805a\u5408 \u5728\u5904\u7406\u6d89\u53ca\u6570\u636e\u805a\u5408\u7684\u67e5\u8be2\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u5c06 count group by \u548c select \u8bed\u53e5\u7ed3\u5408\u4f7f\u7528\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5c06\u4e24\u6761\u8bed\u53e5\u5408\u5e76\u4e3a\u4e00\u6761\uff0c\u4ee5\u9ad8\u6548\u5730\u83b7\u53d6\u6240\u9700\u6570\u636e\u3002 \u95ee\u9898\u5185\u5bb9\uff1a \u4ee5 tb \u8868\u4e3a\u4f8b\uff0c\u5176\u7ed3\u6784\u5982\u4e0b\uff1a a b 1 2 1 2 1 3 \u671f\u671b\u5f97\u5230\u4ee5\u4e0b\u7ed3\u679c\uff1a a b num 1 2 2 1 2 2 1 3 1 \u89e3\u7b54\uff1a \u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b sql \u8bed\u53e5\u5c06 count group by \u548c [&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-46483","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46483","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=46483"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46483\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}