{"id":27542,"date":"2024-11-24T13:22:11","date_gmt":"2024-11-24T05:22:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27542\/"},"modified":"2024-11-24T13:22:11","modified_gmt":"2024-11-24T05:22:11","slug":"sql-%e5%ad%90%e5%8f%a5%e6%89%a7%e8%a1%8c%e9%a1%ba%e5%ba%8f%ef%bc%9a%e5%85%88-having-%e8%bf%98%e6%98%af%e5%85%88-select%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27542\/","title":{"rendered":"SQL \u5b50\u53e5\u6267\u884c\u987a\u5e8f\uff1a\u5148 HAVING \u8fd8\u662f\u5148 SELECT\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173128808047202.jpg\" class=\"aligncenter\" title=\"SQL \u5b50\u53e5\u6267\u884c\u987a\u5e8f\uff1a\u5148 HAVING \u8fd8\u662f\u5148 SELECT\uff1f\u63d2\u56fe\" alt=\"SQL \u5b50\u53e5\u6267\u884c\u987a\u5e8f\uff1a\u5148 HAVING \u8fd8\u662f\u5148 SELECT\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5148 having \u8fd8\u662f\u5148 select\uff1fsql \u5b50\u53e5\u6267\u884c\u987a\u5e8f\u63ed\u79d8<\/strong><\/p>\n<p>sql \u8bed\u53e5\u4e2d\uff0chaving \u5b50\u53e5\u7528\u4e8e\u7b5b\u9009\u7ec4\u5185\u7684\u8bb0\u5f55\uff0c\u800c select \u5b50\u53e5\u7528\u4e8e\u9009\u62e9\u8981\u663e\u793a\u7684\u5217\u3002\u90a3\u4e48\uff0c\u662f\u5148\u6267\u884c having \u5b50\u53e5\u8fd8\u662f select \u5b50\u53e5\u5462\uff1f<\/p>\n<p>\u4f20\u7edf\u89c2\u70b9\u8ba4\u4e3a\uff0c\u5148\u6267\u884c having \u5b50\u53e5\uff0c\u7136\u540e\u518d\u6267\u884c select \u5b50\u53e5\u3002\u7136\u800c\uff0c\u793a\u4f8b\u4ee3\u7801\u4e2d\u4f3c\u4e4e\u4e0e\u8fd9\u4e00\u89c2\u70b9\u76f8\u6096\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT sum(num) AS all_num\nFROM table\nGROUP BY id\nHAVING all_num &gt; 50<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6267\u884c\u7ed3\u679c\u663e\u793a\uff0c\u5148\u6267\u884c\u4e86 select \u8bed\u53e5\uff0c\u5373\u8ba1\u7b97\u4e86 all_num\uff0c\u7136\u540e\u518d\u6267\u884c having \u5b50\u53e5\uff0c\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u4e86\u7b5b\u9009\u3002<\/p>\n<p><strong>\u539f\u56e0<\/strong><\/p>\n<p>\u8fd9\u79cd\u73b0\u8c61\u80cc\u540e\u7684\u539f\u56e0\u5e76\u975e having \u5b50\u53e5\u4f18\u5148\u6267\u884c\uff0c\u800c\u662f\u56e0\u4e3a select \u8bed\u53e5\u5728 sql \u4e2d\u5177\u6709\u53cc\u91cd\u4f5c\u7528\u3002<\/p>\n<p>\u4e00\u65b9\u9762\uff0cselect \u5b50\u53e5\u53ef\u4ee5\u7528\u4e8e\u9009\u62e9\u8981\u663e\u793a\u7684\u5217\u3002\u4f46\u5728\u672c\u4f8b\u4e2d\uff0cselect \u8bed\u53e5\u6ca1\u6709\u9009\u62e9\u4efb\u4f55\u73b0\u6709\u5217\uff0c\u800c\u662f\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u5217 all_num\u3002\u56e0\u6b64\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cselect \u8bed\u53e5\u4e0d\u6267\u884c\u4efb\u4f55\u9009\u62e9\u6027\u64cd\u4f5c\u3002<\/p>\n<p>\u53e6\u4e00\u65b9\u9762\uff0cselect \u8bed\u53e5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u3002\u672c\u4f8b\u4e2d\uff0cselect \u8bed\u53e5\u8ba1\u7b97\u4e86\u6bcf\u4e2a\u7ec4\u7684 num \u5217\u7684\u603b\u548c\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728 all_num \u5217\u4e2d\u3002\u56e0\u6b64\uff0c\u5728\u6b64\u573a\u666f\u4e0b\uff0cselect \u8bed\u53e5\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u805a\u5408\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u6267\u884c\u987a\u5e8f<\/strong><\/p>\n<p>\u5f53 select \u8bed\u53e5\u5305\u542b\u805a\u5408\u51fd\u6570\u65f6\uff0csql \u5f15\u64ce\u7684\u6267\u884c\u987a\u5e8f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5206\u7ec4\uff08group by \u5b50\u53e5\uff09<\/li>\n<li>\u805a\u5408\uff08select \u5b50\u53e5\u4e2d\u7684\u805a\u5408\u51fd\u6570\uff09<\/li>\n<li>\u7b5b\u9009\uff08having \u5b50\u53e5\uff09<\/li>\n<li>\u9009\u62e9\uff08select \u5b50\u53e5\u9009\u62e9\u8981\u663e\u793a\u7684\u5217\uff09<\/li>\n<\/ol>\n<p>\u56e0\u6b64\uff0c\u5728\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6b63\u786e\u7684\u6267\u884c\u987a\u5e8f\u662f\uff1a<\/p>\n<ol>\n<li>\u6309 id \u5217\u5206\u7ec4<\/li>\n<li>\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u7684 num \u5217\u7684\u603b\u548c\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728 all_num \u5217\u4e2d<\/li>\n<li>\u7b5b\u9009\u51fa all_num \u5927\u4e8e 50 \u7684\u7ec4<\/li>\n<li>\u9009\u62e9 all_num \u5217\u663e\u793a\u7ed3\u679c<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662fSQL \u5b50\u53e5\u6267\u884c\u987a\u5e8f\uff1a\u5148 HAVING \u8fd8\u662f\u5148 SELECT\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5148 having \u8fd8\u662f\u5148 select\uff1fsql \u5b50\u53e5\u6267\u884c\u987a\u5e8f\u63ed\u79d8 sql \u8bed\u53e5\u4e2d\uff0chaving \u5b50\u53e5\u7528\u4e8e\u7b5b\u9009\u7ec4\u5185\u7684\u8bb0\u5f55\uff0c\u800c select \u5b50\u53e5\u7528\u4e8e\u9009\u62e9\u8981\u663e\u793a\u7684\u5217\u3002\u90a3\u4e48\uff0c\u662f\u5148\u6267\u884c having \u5b50\u53e5\u8fd8\u662f select \u5b50\u53e5\u5462\uff1f \u4f20\u7edf\u89c2\u70b9\u8ba4\u4e3a\uff0c\u5148\u6267\u884c having \u5b50\u53e5\uff0c\u7136\u540e\u518d\u6267\u884c select \u5b50\u53e5\u3002\u7136\u800c\uff0c\u793a\u4f8b\u4ee3\u7801\u4e2d\u4f3c\u4e4e\u4e0e\u8fd9\u4e00\u89c2\u70b9\u76f8\u6096\u3002\u4ee3\u7801\u5982\u4e0b\uff1a SELECT sum(num) AS all_num FROM table GROUP BY id HAVING all_num &gt; 50 \u767b\u5f55\u540e\u590d\u5236 \u6267\u884c\u7ed3\u679c\u663e\u793a\uff0c\u5148\u6267\u884c\u4e86 select \u8bed\u53e5\uff0c\u5373\u8ba1\u7b97\u4e86 all_num\uff0c\u7136\u540e\u518d\u6267\u884c having \u5b50\u53e5\uff0c\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u4e86\u7b5b\u9009\u3002 \u539f\u56e0 \u8fd9\u79cd\u73b0\u8c61\u80cc\u540e\u7684\u539f\u56e0\u5e76\u975e having \u5b50\u53e5\u4f18\u5148\u6267\u884c\uff0c\u800c\u662f\u56e0\u4e3a select \u8bed\u53e5\u5728 sql \u4e2d\u5177\u6709\u53cc\u91cd\u4f5c\u7528\u3002 \u4e00\u65b9\u9762\uff0cselect \u5b50\u53e5\u53ef\u4ee5\u7528\u4e8e\u9009\u62e9\u8981\u663e\u793a\u7684\u5217\u3002\u4f46\u5728\u672c\u4f8b\u4e2d\uff0cselect \u8bed\u53e5\u6ca1\u6709\u9009\u62e9\u4efb\u4f55\u73b0\u6709\u5217\uff0c\u800c\u662f\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u5217 all_num\u3002\u56e0\u6b64\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cselect \u8bed\u53e5\u4e0d\u6267\u884c\u4efb\u4f55\u9009\u62e9\u6027\u64cd\u4f5c\u3002 \u53e6\u4e00\u65b9\u9762\uff0cselect \u8bed\u53e5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u3002\u672c\u4f8b\u4e2d\uff0cselect \u8bed\u53e5\u8ba1\u7b97\u4e86\u6bcf\u4e2a\u7ec4\u7684 [&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-27542","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27542","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=27542"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27542\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}