{"id":5214,"date":"2024-11-14T11:58:02","date_gmt":"2024-11-14T03:58:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5214\/"},"modified":"2024-11-14T11:58:02","modified_gmt":"2024-11-14T03:58:02","slug":"sql%e8%af%ad%e5%8f%a5%e4%b8%adhaving%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5214\/","title":{"rendered":"sql\u8bed\u53e5\u4e2dhaving\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  having \u5b50\u53e5\u7528\u4e8e\u5728\u805a\u96c6\u67e5\u8be2\u4e2d\u8fc7\u6ee4\u5206\u7ec4\u540e\u7684\u6570\u636e\uff0c\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u57fa\u4e8e\u7279\u5b9a\u6761\u4ef6\u9009\u62e9\u6216\u6392\u9664\u5206\u7ec4\u7ed3\u679c\uff0c\u4ece\u800c\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u548c\u62a5\u544a\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/24\/2024052421212055287.jpg\" class=\"aligncenter\" title=\"sql\u8bed\u53e5\u4e2dhaving\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"sql\u8bed\u53e5\u4e2dhaving\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>HAVING \u5b50\u53e5\u5728 SQL \u4e2d\u7684\u5e94\u7528<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f HAVING \u5b50\u53e5\uff1f<\/strong><\/p>\n<p>HAVING \u5b50\u53e5\u7528\u4e8e\u5728\u805a\u96c6\u67e5\u8be2\uff08\u4f7f\u7528 GROUP BY \u5b50\u53e5\uff09\u4e2d\u5bf9\u5206\u7ec4\u540e\u7684\u6570\u636e\u5e94\u7528\u8fc7\u6ee4\u5668\u3002\u5b83\u4e0e WHERE \u5b50\u53e5\u7c7b\u4f3c\uff0c\u4f46\u540e\u8005\u53ea\u8fc7\u6ee4\u539f\u59cb\u6570\u636e\uff0c\u800c HAVING \u5219\u8fc7\u6ee4\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT \u805a\u96c6\u51fd\u6570(\u5217\u540d)\nFROM \u8868\u540d\nGROUP BY \u5217\u540d\nHAVING \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\u5728\u5206\u7ec4\u540e\u5224\u65ad\u662f\u5426\u5305\u542b\u6216\u6392\u9664\u5206\u7ec4\u7ed3\u679c\u3002\u6761\u4ef6\u901a\u5e38\u57fa\u4e8e\u805a\u96c6\u51fd\u6570\uff08\u5982 SUM\u3001COUNT\u3001AVG\uff09\uff0c\u5e76\u6307\u5b9a\u4e00\u4e2a\u9608\u503c\u6216\u8303\u56f4\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u67e5\u627e\u8ba2\u5355\u91d1\u989d\u5927\u4e8e 1000 \u7f8e\u5143\u7684\u5ba2\u6237\uff1a<\/p>\n<pre>SELECT COUNT(DISTINCT customer_id)\nFROM orders\nGROUP BY customer_id\nHAVING SUM(order_amount) &gt; 1000<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u67e5\u627e\u6bcf\u4e2a\u90e8\u95e8\u5e73\u5747\u5de5\u8d44\u5927\u4e8e 60000 \u7f8e\u5143\u7684\u90e8\u95e8\uff1a<\/p>\n<pre>SELECT department_name, AVG(salary)\nFROM employees\nGROUP BY department_name\nHAVING AVG(salary) &gt; 60000<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>HAVING \u5b50\u53e5\u4e0e WHERE \u5b50\u53e5\u7684\u533a\u522b\uff1a<\/strong><\/p>\n<ul>\n<li>WHERE \u5b50\u53e5\u8fc7\u6ee4\u539f\u59cb\u6570\u636e\u884c\uff0cHAVING \u5b50\u53e5\u8fc7\u6ee4\u5206\u7ec4\u540e\u7684\u6570\u636e\u3002<\/li>\n<li>WHERE \u5b50\u53e5\u5728\u4ece\u6570\u636e\u5e93\u4e2d\u68c0\u7d22\u6570\u636e\u65f6\u4f7f\u7528\uff0cHAVING \u5b50\u53e5\u5728\u5bf9\u5206\u7ec4\u7ed3\u679c\u8fdb\u884c\u64cd\u4f5c\u65f6\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>HAVING \u5b50\u53e5\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u5728\u805a\u96c6\u67e5\u8be2\u4e2d\u5bf9\u5206\u7ec4\u540e\u7684\u6570\u636e\u5e94\u7528\u8fc7\u6ee4\u5668\u3002\u5b83\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u9009\u62e9\u6216\u6392\u9664\u5206\u7ec4\u7ed3\u679c\uff0c\u4ece\u800c\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u548c\u62a5\u544a\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e2dhaving\u600e\u4e48\u7528\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>having \u5b50\u53e5\u7528\u4e8e\u5728\u805a\u96c6\u67e5\u8be2\u4e2d\u8fc7\u6ee4\u5206\u7ec4\u540e\u7684\u6570\u636e\uff0c\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u57fa\u4e8e\u7279\u5b9a\u6761\u4ef6\u9009\u62e9\u6216\u6392\u9664\u5206\u7ec4\u7ed3\u679c\uff0c\u4ece\u800c\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u548c\u62a5\u544a\u3002 HAVING \u5b50\u53e5\u5728 SQL \u4e2d\u7684\u5e94\u7528 \u4ec0\u4e48\u662f HAVING \u5b50\u53e5\uff1f HAVING \u5b50\u53e5\u7528\u4e8e\u5728\u805a\u96c6\u67e5\u8be2\uff08\u4f7f\u7528 GROUP BY \u5b50\u53e5\uff09\u4e2d\u5bf9\u5206\u7ec4\u540e\u7684\u6570\u636e\u5e94\u7528\u8fc7\u6ee4\u5668\u3002\u5b83\u4e0e WHERE \u5b50\u53e5\u7c7b\u4f3c\uff0c\u4f46\u540e\u8005\u53ea\u8fc7\u6ee4\u539f\u59cb\u6570\u636e\uff0c\u800c HAVING \u5219\u8fc7\u6ee4\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u3002 \u8bed\u6cd5\uff1a SELECT \u805a\u96c6\u51fd\u6570(\u5217\u540d) FROM \u8868\u540d GROUP BY \u5217\u540d HAVING \u6761\u4ef6 \u767b\u5f55\u540e\u590d\u5236 \u7528\u6cd5\uff1a HAVING \u5b50\u53e5\u7528\u4e8e\u5728\u5206\u7ec4\u540e\u5224\u65ad\u662f\u5426\u5305\u542b\u6216\u6392\u9664\u5206\u7ec4\u7ed3\u679c\u3002\u6761\u4ef6\u901a\u5e38\u57fa\u4e8e\u805a\u96c6\u51fd\u6570\uff08\u5982 SUM\u3001COUNT\u3001AVG\uff09\uff0c\u5e76\u6307\u5b9a\u4e00\u4e2a\u9608\u503c\u6216\u8303\u56f4\u3002 \u793a\u4f8b\uff1a \u67e5\u627e\u8ba2\u5355\u91d1\u989d\u5927\u4e8e 1000 \u7f8e\u5143\u7684\u5ba2\u6237\uff1a SELECT COUNT(DISTINCT customer_id) FROM orders GROUP BY customer_id HAVING SUM(order_amount) &gt; 1000 \u767b\u5f55\u540e\u590d\u5236 \u67e5\u627e\u6bcf\u4e2a\u90e8\u95e8\u5e73\u5747\u5de5\u8d44\u5927\u4e8e 60000 \u7f8e\u5143\u7684\u90e8\u95e8\uff1a SELECT department_name, [&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-5214","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5214","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=5214"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5214\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}