{"id":6769,"date":"2024-11-14T13:11:50","date_gmt":"2024-11-14T05:11:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6769\/"},"modified":"2024-11-14T13:11:50","modified_gmt":"2024-11-14T05:11:50","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:\/\/fwq.ai\/blog\/6769\/","title":{"rendered":"\u5982\u4f55\u5c06 COUNT GROUP BY \u548c SELECT \u67e5\u8be2\u5408\u5e76\u4e3a\u4e00\u6761\u8bed\u53e5\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172991216185196.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06 COUNT GROUP BY \u548c SELECT \u67e5\u8be2\u5408\u5e76\u4e3a\u4e00\u6761\u8bed\u53e5\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06 COUNT GROUP BY \u548c SELECT \u67e5\u8be2\u5408\u5e76\u4e3a\u4e00\u6761\u8bed\u53e5\uff1f\u63d2\u56fe\" \/><\/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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c06 COUNT GROUP BY \u548c SELECT \u67e5\u8be2\u5408\u5e76\u4e3a\u4e00\u6761\u8bed\u53e5\uff1f\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>\u5408\u5e76 count group by \u548c select \u67e5\u8be2 \u8868 tb \u7684\u7ed3\u6784\u5982\u4e0b\uff1a a b 1 2 1 2 1 3 \u76ee\u6807\u662f\u5f97\u5230\u5982\u4e0b\u6570\u636e\uff1a a b num(a,b\u5217\u76f8\u540c\u7684\u884c\u6570) 1 2 2 1 2 2 1 3 1 \u539f\u59cb\u7684\u4e24\u4e2a\u67e5\u8be2\u5206\u522b\u4f7f\u7528 count group by \u548c select \u8bed\u53e5\uff1a select count(*) as num from tb group by a,b select * from tb \u767b\u5f55\u540e\u590d\u5236 \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 select t1.a,t1.b,t2.count [&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-6769","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6769","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=6769"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6769\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}