{"id":6409,"date":"2024-11-14T13:17:20","date_gmt":"2024-11-14T05:17:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6409\/"},"modified":"2024-11-14T13:17:20","modified_gmt":"2024-11-14T05:17:20","slug":"mysql-having%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6409\/","title":{"rendered":"mysql having\u7528\u6cd5"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\uff0chaving \u5b50\u53e5\u7528\u4e8e\u7b5b\u9009\u805a\u5408\u51fd\u6570\u7684\u7ed3\u679c\uff0c\u4ec5\u5f71\u54cd\u805a\u5408\u503c\uff0c\u800c\u975e\u539f\u59cb\u6570\u636e\u3002\u5b83\u53ef\u4ee5\u8fc7\u6ee4\u805a\u5408\u7ed3\u679c\uff08\u6309\u805a\u5408\u503c\uff09\u3001\u5e94\u7528\u591a\u4e2a\u7b5b\u9009\u6761\u4ef6\u6216\u5bf9\u805a\u5408\u7ed3\u679c\u6392\u5e8f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/01\/2024080121433112762.jpg\" class=\"aligncenter\" title=\"mysql having\u7528\u6cd5\u63d2\u56fe\" alt=\"mysql having\u7528\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>HAVING \u5b50\u53e5\u5728 MySQL \u4e2d\u7684\u7528\u6cd5<\/strong><\/p>\n<p><strong>HAVING \u5b50\u53e5<\/strong>\u662f\u5728 MySQL \u4e2d\u7528\u4e8e\u7b5b\u9009\u805a\u5408\u7ed3\u679c\uff08\u4f8b\u5982 SUM\u3001COUNT\u3001AVG \u7b49\uff09\u7684\u884c\u3002\u5b83\u7c7b\u4f3c\u4e8e WHERE \u5b50\u53e5\uff0c\u4f46\u5b83\u662f\u5728\u6267\u884c\u540e\u518d\u5e94\u7528\u7684\uff0c\u56e0\u6b64\u53ea\u80fd\u5f15\u7528\u805a\u5408\u51fd\u6570\u548c\u5206\u7ec4\u5217\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT \u805a\u5408\u51fd\u6570(\u5217\u540d)\nFROM \u8868\u540d\nGROUP BY \u5206\u7ec4\u5217\nHAVING \u7b5b\u9009\u6761\u4ef6;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<p>HAVING \u5b50\u53e5\u7528\u4e8e\u4ee5\u4e0b\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li> <strong>\u8fc7\u6ee4\u805a\u5408\u7ed3\u679c\uff1a<\/strong>\u6839\u636e\u805a\u5408\u51fd\u6570\u7684\u503c\u7b5b\u9009\u884c\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u9500\u552e\u989d\u8d85\u8fc7 1000 \u7f8e\u5143\u7684\u8ba2\u5355\u3002<\/li>\n<li> <strong>\u5e94\u7528\u591a\u4e2a\u7b5b\u9009\u6761\u4ef6\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a HAVING \u5b50\u53e5\u6765\u5e94\u7528\u590d\u6742\u7684\u7b5b\u9009\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u9500\u552e\u989d\u8d85\u8fc7 1000 \u7f8e\u5143\u4e14\u5ba2\u6237\u7c7b\u578b\u4e3a\u201c\u6279\u53d1\u201d\u7684\u8ba2\u5355\u3002<\/li>\n<li> <strong>\u5bf9\u805a\u5408\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528 HAVING \u5b50\u53e5\u5bf9\u805a\u5408\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u6309\u9500\u552e\u989d\u4ece\u9ad8\u5230\u4f4e\u6392\u5e8f\u8ba2\u5355\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u67e5\u627e\u9500\u552e\u989d\u8d85\u8fc7 1000 \u7f8e\u5143\u7684\u8ba2\u5355\uff1a<\/p>\n<pre>SELECT SUM(\u9500\u552e\u989d)\nFROM \u8ba2\u5355\u8868\nGROUP BY \u8ba2\u5355\u53f7\nHAVING SUM(\u9500\u552e\u989d) &gt; 1000;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u67e5\u627e\u9500\u552e\u989d\u8d85\u8fc7 1000 \u7f8e\u5143\u4e14\u5ba2\u6237\u7c7b\u578b\u4e3a\u201c\u6279\u53d1\u201d\u7684\u8ba2\u5355\uff1a<\/p>\n<pre>SELECT SUM(\u9500\u552e\u989d)\nFROM \u8ba2\u5355\u8868\nGROUP BY \u8ba2\u5355\u53f7\nHAVING SUM(\u9500\u552e\u989d) &gt; 1000\nAND \u5ba2\u6237\u7c7b\u578b = '\u6279\u53d1';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6309\u9500\u552e\u989d\u4ece\u9ad8\u5230\u4f4e\u6392\u5e8f\u8ba2\u5355\uff1a<\/p>\n<pre>SELECT SUM(\u9500\u552e\u989d)\nFROM \u8ba2\u5355\u8868\nGROUP BY \u8ba2\u5355\u53f7\nHAVING SUM(\u9500\u552e\u989d) &gt; 0\nORDER BY SUM(\u9500\u552e\u989d) DESC;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f having\u7528\u6cd5\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>\u5728 mysql \u4e2d\uff0chaving \u5b50\u53e5\u7528\u4e8e\u7b5b\u9009\u805a\u5408\u51fd\u6570\u7684\u7ed3\u679c\uff0c\u4ec5\u5f71\u54cd\u805a\u5408\u503c\uff0c\u800c\u975e\u539f\u59cb\u6570\u636e\u3002\u5b83\u53ef\u4ee5\u8fc7\u6ee4\u805a\u5408\u7ed3\u679c\uff08\u6309\u805a\u5408\u503c\uff09\u3001\u5e94\u7528\u591a\u4e2a\u7b5b\u9009\u6761\u4ef6\u6216\u5bf9\u805a\u5408\u7ed3\u679c\u6392\u5e8f\u3002 HAVING \u5b50\u53e5\u5728 MySQL \u4e2d\u7684\u7528\u6cd5 HAVING \u5b50\u53e5\u662f\u5728 MySQL \u4e2d\u7528\u4e8e\u7b5b\u9009\u805a\u5408\u7ed3\u679c\uff08\u4f8b\u5982 SUM\u3001COUNT\u3001AVG \u7b49\uff09\u7684\u884c\u3002\u5b83\u7c7b\u4f3c\u4e8e WHERE \u5b50\u53e5\uff0c\u4f46\u5b83\u662f\u5728\u6267\u884c\u540e\u518d\u5e94\u7528\u7684\uff0c\u56e0\u6b64\u53ea\u80fd\u5f15\u7528\u805a\u5408\u51fd\u6570\u548c\u5206\u7ec4\u5217\u3002 \u8bed\u6cd5\uff1a SELECT \u805a\u5408\u51fd\u6570(\u5217\u540d) FROM \u8868\u540d GROUP BY \u5206\u7ec4\u5217 HAVING \u7b5b\u9009\u6761\u4ef6; \u767b\u5f55\u540e\u590d\u5236 \u7528\u6cd5\uff1a HAVING \u5b50\u53e5\u7528\u4e8e\u4ee5\u4e0b\u60c5\u51b5\uff1a \u8fc7\u6ee4\u805a\u5408\u7ed3\u679c\uff1a\u6839\u636e\u805a\u5408\u51fd\u6570\u7684\u503c\u7b5b\u9009\u884c\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u9500\u552e\u989d\u8d85\u8fc7 1000 \u7f8e\u5143\u7684\u8ba2\u5355\u3002 \u5e94\u7528\u591a\u4e2a\u7b5b\u9009\u6761\u4ef6\uff1a\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a HAVING \u5b50\u53e5\u6765\u5e94\u7528\u590d\u6742\u7684\u7b5b\u9009\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u9500\u552e\u989d\u8d85\u8fc7 1000 \u7f8e\u5143\u4e14\u5ba2\u6237\u7c7b\u578b\u4e3a\u201c\u6279\u53d1\u201d\u7684\u8ba2\u5355\u3002 \u5bf9\u805a\u5408\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\uff1a\u53ef\u4ee5\u4f7f\u7528 HAVING \u5b50\u53e5\u5bf9\u805a\u5408\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u6309\u9500\u552e\u989d\u4ece\u9ad8\u5230\u4f4e\u6392\u5e8f\u8ba2\u5355\u3002 \u793a\u4f8b\uff1a \u67e5\u627e\u9500\u552e\u989d\u8d85\u8fc7 1000 \u7f8e\u5143\u7684\u8ba2\u5355\uff1a SELECT SUM(\u9500\u552e\u989d) FROM \u8ba2\u5355\u8868 GROUP BY \u8ba2\u5355\u53f7 HAVING SUM(\u9500\u552e\u989d) &gt; [&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-6409","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6409","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=6409"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6409\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}