{"id":6650,"date":"2024-11-14T13:16:31","date_gmt":"2024-11-14T05:16:31","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6650\/"},"modified":"2024-11-14T13:16:31","modified_gmt":"2024-11-14T05:16:31","slug":"group-by-having%e7%94%a8%e6%b3%95%e4%b8%be%e4%be%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6650\/","title":{"rendered":"group by having\u7528\u6cd5\u4e3e\u4f8b"},"content":{"rendered":"<blockquote><p>\n  groupby having\u5b50\u53e5\u7528\u4e8e\u5bf9\u5206\u7ec4\u540e\u7684\u6570\u636e\u8fdb\u884c\u7b5b\u9009\u3002\u5b83\u9996\u5148\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u6839\u636e\u6307\u5b9a\u6761\u4ef6\u8fc7\u6ee4\u7ed3\u679c\u3002\u5e38\u89c1\u7528\u6cd5\u5305\u62ec\uff1a\u627e\u51fa\u7279\u5b9a\u5206\u7ec4\u7684\u603b\u548c\u6216\u5e73\u5747\u503c\u3002\u67e5\u627e\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5206\u7ec4\u3002\u5bf9\u5206\u7ec4\u6570\u636e\u8fdb\u884c\u805a\u5408\u8ba1\u7b97\uff0c\u4f8b\u5982\u6c42\u548c\u3001\u6c42\u5e73\u5747\u503c\u6216\u8ba1\u6570\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202409\/11\/2024091107280071562.jpg\" class=\"aligncenter\" title=\"group by having\u7528\u6cd5\u4e3e\u4f8b\u63d2\u56fe\" alt=\"group by having\u7528\u6cd5\u4e3e\u4f8b\u63d2\u56fe\" \/><\/p>\n<p><strong>GroupBy Having\u7528\u6cd5\u4e3e\u4f8b<\/strong><\/p>\n<p>GroupBy Having\u5b50\u53e5\u662fSQL\u4e2d\u7528\u6765\u5bf9\u5206\u7ec4\u540e\u7684\u6570\u636e\u8fdb\u884c\u7b5b\u9009\u7684\u8bed\u6cd5\u3002\u5b83\u4e0eGroupBy\u5b50\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u5148\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u8fc7\u6ee4\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT\n    \u5217\u540d1,\n    \u5217\u540d2,\n    ...\nFROM\n    \u8868\u540d\nGROUP BY\n    \u5206\u7ec4\u5217\nHAVING\n    \u7b5b\u9009\u6761\u4ef6<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u7528\u6cd5\u4e3e\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u4ea7\u54c1\u9500\u552e\u6570\u636e\u7684\u8868sales\uff0c\u8be5\u8868\u5305\u542bproduct_id\u3001product_name\u3001quantity\u548ctotal_sales\u5217\u3002\u73b0\u5728\uff0c\u6211\u4eec\u60f3\u8981\u627e\u51fa\u603b\u9500\u552e\u989d\u8d85\u8fc710000\u7684\u6bcf\u4e2a\u4ea7\u54c1\u7684\u6570\u91cf\u3002<\/p>\n<p><strong>\u67e5\u8be2\uff1a<\/strong><\/p>\n<pre>SELECT\n    product_id,\n    product_name,\n    COUNT(DISTINCT quantity) AS product_count\nFROM\n    sales\nGROUP BY\n    product_id\nHAVING\n    SUM(total_sales) &gt; 10000<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u8fd9\u4e2a\u67e5\u8be2\u5982\u4f55\u8fd0\u4f5c\uff1f<\/strong><\/p>\n<ol>\n<li> <strong>GroupBy\u5b50\u53e5<\/strong>\uff1a\u5b83\u6839\u636eproduct_id\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u5c06\u5177\u6709\u76f8\u540c\u4ea7\u54c1ID\u7684\u8bb0\u5f55\u5206\u7ec4\u5728\u4e00\u8d77\u3002<\/li>\n<li> <strong>Having\u5b50\u53e5<\/strong>\uff1a\u5b83\u68c0\u67e5\u6bcf\u4e2a\u5206\u7ec4\uff0c\u5e76\u6839\u636e\u6761\u4ef6SUM(total_sales) &gt; 10000\u5bf9\u5176\u8fdb\u884c\u7b5b\u9009\u3002\u53ea\u6709\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u7ec4\u624d\u4f1a\u88ab\u8fd4\u56de\u3002<\/li>\n<li> <strong>COUNT(DISTINCT quantity)<\/strong>\uff1a\u5b83\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u4e2dquantity\u5217\u7684\u552f\u4e00\u503c\u6570\u91cf\uff0c\u8fd9\u8868\u793a\u4e86\u6bcf\u4e2a\u4ea7\u54c1\u9500\u552e\u7684\u6b21\u6570\u3002<\/li>\n<\/ol>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<p>\u8be5\u67e5\u8be2\u5c06\u8fd4\u56de\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u7684\u6bcf\u4e2a\u4ea7\u54c1\u7684\u6570\u91cf\uff1a<\/p>\n<ul>\n<li>\u603b\u9500\u552e\u989d\u8d85\u8fc710000\u7f8e\u5143<\/li>\n<\/ul>\n<p>\u8f93\u51fa\u53ef\u80fd\u5982\u4e0b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>product_id<\/th>\n<th>product_name<\/th>\n<th>product_count<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>Product A<\/td>\n<td>10<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Product B<\/td>\n<td>5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ee5\u4e0a\u5c31\u662fgroup by having\u7528\u6cd5\u4e3e\u4f8b\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>groupby having\u5b50\u53e5\u7528\u4e8e\u5bf9\u5206\u7ec4\u540e\u7684\u6570\u636e\u8fdb\u884c\u7b5b\u9009\u3002\u5b83\u9996\u5148\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u6839\u636e\u6307\u5b9a\u6761\u4ef6\u8fc7\u6ee4\u7ed3\u679c\u3002\u5e38\u89c1\u7528\u6cd5\u5305\u62ec\uff1a\u627e\u51fa\u7279\u5b9a\u5206\u7ec4\u7684\u603b\u548c\u6216\u5e73\u5747\u503c\u3002\u67e5\u627e\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5206\u7ec4\u3002\u5bf9\u5206\u7ec4\u6570\u636e\u8fdb\u884c\u805a\u5408\u8ba1\u7b97\uff0c\u4f8b\u5982\u6c42\u548c\u3001\u6c42\u5e73\u5747\u503c\u6216\u8ba1\u6570\u3002 GroupBy Having\u7528\u6cd5\u4e3e\u4f8b GroupBy Having\u5b50\u53e5\u662fSQL\u4e2d\u7528\u6765\u5bf9\u5206\u7ec4\u540e\u7684\u6570\u636e\u8fdb\u884c\u7b5b\u9009\u7684\u8bed\u6cd5\u3002\u5b83\u4e0eGroupBy\u5b50\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u5148\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u8fc7\u6ee4\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u3002 \u8bed\u6cd5\uff1a SELECT \u5217\u540d1, \u5217\u540d2, &#8230; FROM \u8868\u540d GROUP BY \u5206\u7ec4\u5217 HAVING \u7b5b\u9009\u6761\u4ef6 \u767b\u5f55\u540e\u590d\u5236 \u7528\u6cd5\u4e3e\u4f8b\uff1a \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u4ea7\u54c1\u9500\u552e\u6570\u636e\u7684\u8868sales\uff0c\u8be5\u8868\u5305\u542bproduct_id\u3001product_name\u3001quantity\u548ctotal_sales\u5217\u3002\u73b0\u5728\uff0c\u6211\u4eec\u60f3\u8981\u627e\u51fa\u603b\u9500\u552e\u989d\u8d85\u8fc710000\u7684\u6bcf\u4e2a\u4ea7\u54c1\u7684\u6570\u91cf\u3002 \u67e5\u8be2\uff1a SELECT product_id, product_name, COUNT(DISTINCT quantity) AS product_count FROM sales GROUP BY product_id HAVING SUM(total_sales) &gt; 10000 \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4e2a\u67e5\u8be2\u5982\u4f55\u8fd0\u4f5c\uff1f GroupBy\u5b50\u53e5\uff1a\u5b83\u6839\u636eproduct_id\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u5c06\u5177\u6709\u76f8\u540c\u4ea7\u54c1ID\u7684\u8bb0\u5f55\u5206\u7ec4\u5728\u4e00\u8d77\u3002 Having\u5b50\u53e5\uff1a\u5b83\u68c0\u67e5\u6bcf\u4e2a\u5206\u7ec4\uff0c\u5e76\u6839\u636e\u6761\u4ef6SUM(total_sales) &gt; 10000\u5bf9\u5176\u8fdb\u884c\u7b5b\u9009\u3002\u53ea\u6709\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u7ec4\u624d\u4f1a\u88ab\u8fd4\u56de\u3002 COUNT(DISTINCT quantity)\uff1a\u5b83\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u4e2dquantity\u5217\u7684\u552f\u4e00\u503c\u6570\u91cf\uff0c\u8fd9\u8868\u793a\u4e86\u6bcf\u4e2a\u4ea7\u54c1\u9500\u552e\u7684\u6b21\u6570\u3002 \u7ed3\u679c\uff1a \u8be5\u67e5\u8be2\u5c06\u8fd4\u56de\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u7684\u6bcf\u4e2a\u4ea7\u54c1\u7684\u6570\u91cf\uff1a \u603b\u9500\u552e\u989d\u8d85\u8fc710000\u7f8e\u5143 \u8f93\u51fa\u53ef\u80fd\u5982\u4e0b\uff1a product_id product_name product_count 1 Product [&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-6650","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6650","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=6650"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6650\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}