{"id":200303,"date":"2025-05-06T11:03:38","date_gmt":"2025-05-06T03:03:38","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200303\/"},"modified":"2025-05-06T11:03:38","modified_gmt":"2025-05-06T03:03:38","slug":"%e5%a6%82%e4%bd%95%e5%b0%86-count-group-by-%e5%92%8c-select-%e6%9f%a5%e8%af%a2%e5%90%88%e5%b9%b6%e4%b8%ba%e4%b8%80%e6%9d%a1%e8%af%ad%e5%8f%a5%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200303\/","title":{"rendered":"\u5982\u4f55\u5c06 COUNT GROUP BY \u548c SELECT \u67e5\u8be2\u5408\u5e76\u4e3a\u4e00\u6761\u8bed\u53e5\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5c06 COUNT GROUP BY \u548c SELECT \u67e5\u8be2\u5408\u5e76\u4e3a\u4e00\u6761\u8bed\u53e5\uff1f<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241101\/173042356867242b103a55b.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5408\u5e76 count group by \u548c select \u67e5\u8be2<\/strong><\/p>\n<p>\u8868 tb \u7684\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>\u76ee\u6807\u662f\u5f97\u5230\u5982\u4e0b\u6570\u636e\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>a<\/th>\n<th>b<\/th>\n<th>num(a,b\u5217\u76f8\u540c\u7684\u884c\u6570)<\/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>\u539f\u59cb\u7684\u4e24\u4e2a\u67e5\u8be2\u5206\u522b\u4f7f\u7528 count group by \u548c select \u8bed\u53e5\uff1a<\/p>\n<pre>select count(*) as num from tb group by a,b\n\nselect * from tb<\/pre>\n<p>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u8fde\u63a5\u67e5\u8be2\u5c06\u8fd9\u4e24\u4e2a\u67e5\u8be2\u5408\u5e76\u5230\u4e00\u6761\u8bed\u53e5\u4e2d\uff1a<\/p>\n<pre>select t1.a,t1.b,t2.count from test as t1 left join (select a,b,count(*)as count from test group by a,b) as t2 on t1.a = t2.a and t1.b = t2.b<\/pre>\n<p>\u5728\u5de6\u4fa7\u7684\u8868 t1 \u4e2d\uff0c\u5305\u542b\u6240\u6709\u539f\u59cb\u884c\u3002\u53f3\u4fa7\u7684\u5b50\u67e5\u8be2 (t2) \u4f7f\u7528 count(*) \u51fd\u6570\u5bf9 a \u548c b \u5217\u4e0a\u7684\u552f\u4e00\u7ec4\u5408\u8fdb\u884c\u5206\u7ec4\u8ba1\u6570\u3002\u901a\u8fc7\u4f7f\u7528 left join\uff0c\u539f\u59cb\u884c\u4e0e\u5206\u7ec4\u8ba1\u6570\u76f8\u8fde\u63a5\uff0c\u4ece\u800c\u4e3a\u6bcf\u4e00\u884c\u6dfb\u52a0\u4e86\u4e00\u4e2a num \u5217\uff0c\u8be5\u5217\u5305\u542b a \u548c b \u5217\u76f8\u540c\u884c\u7684\u6570\u91cf\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5982\u4f55\u5c06 COUNT GROUP BY \u548c SELECT \u67e5\u8be2\u5408\u5e76\u4e3a\u4e00\u6761\u8bed\u53e5\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06 COUNT GROUP &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200303","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200303"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200303\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}