{"id":200177,"date":"2025-05-06T12:24:48","date_gmt":"2025-05-06T04:24:48","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200177\/"},"modified":"2025-05-06T12:24:48","modified_gmt":"2025-05-06T04:24:48","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e5%90%8c%e4%b8%80%e5%bc%a0%e8%a1%a8%e5%86%85%e7%9a%84count-group-by%e5%92%8cselect%e8%af%ad%e5%8f%a5%e5%90%88%e5%b9%b6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200177\/","title":{"rendered":"\u5982\u4f55\u5c06\u540c\u4e00\u5f20\u8868\u5185\u7684COUNT GROUP BY\u548cSELECT\u8bed\u53e5\u5408\u5e76\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5c06\u540c\u4e00\u5f20\u8868\u5185\u7684COUNT GROUP BY\u548cSELECT\u8bed\u53e5\u5408\u5e76\uff1f<\/h1>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730009205671dd875074b6.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u540c\u4e00\u5f20\u8868\u5185count group by\u548cselect\u8bed\u53e5\u5408\u5e76<\/strong><\/p>\n<p>\u8981\u5408\u5e76count group by\u548cselect\u8bed\u53e5\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5d4c\u5957\u67e5\u8be2\u6216join\u8bed\u53e5\u3002<\/p>\n<p><strong>\u5d4c\u5957\u67e5\u8be2\u65b9\u6cd5<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u67e5\u8be2\u7684\u65b9\u5f0f\uff0c\u5728\u5916\u90e8\u67e5\u8be2\u4e2d\u4f7f\u7528count group by\u5b50\u67e5\u8be2\u7684\u67e5\u8be2\u7ed3\u679c\u3002<\/p>\n<pre>select a,b,count(*)as count from test\nwhere (a,b) in (select a,b from test group by a,b)<\/pre>\n<p><strong>join\u8bed\u53e5\u65b9\u6cd5<\/strong><\/p>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528join\u8bed\u53e5\u7684\u65b9\u5f0f\uff0c\u5c06count group by\u7684\u67e5\u8be2\u7ed3\u679c\u4e0e\u539f\u59cb\u8868join\u8d77\u6765\u3002<\/p>\n<pre>select t1.a,t1.b,t2.count\nfrom test as t1\nleft join (select a,b,count(*)as count from test group by a,b) as t2\non t1.a = t2.a and t1.b = t2.b<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u5c06\u540c\u4e00\u5f20\u8868\u5185\u7684COUNT GROUP BY\u548cSELECT\u8bed\u53e5\u5408\u5e76\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\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06\u540c\u4e00\u5f20\u8868\u5185\u7684COUNT G&#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-200177","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200177","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=200177"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200177\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}