{"id":7941,"date":"2024-11-14T11:28:23","date_gmt":"2024-11-14T03:28:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7941\/"},"modified":"2024-11-14T11:28:23","modified_gmt":"2024-11-14T03:28:23","slug":"sql%e4%b8%adselect%e4%b8%8ehaving%e5%ad%90%e5%8f%a5%e7%9a%84%e6%89%a7%e8%a1%8c%e9%a1%ba%e5%ba%8f%ef%bc%9a%e5%88%b0%e5%ba%95%e8%b0%81%e5%85%88%e6%89%a7%e8%a1%8c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7941\/","title":{"rendered":"SQL\u4e2dselect\u4e0ehaving\u5b50\u53e5\u7684\u6267\u884c\u987a\u5e8f\uff1a\u5230\u5e95\u8c01\u5148\u6267\u884c\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173139128585071.jpg\" class=\"aligncenter\" title=\"SQL\u4e2dselect\u4e0ehaving\u5b50\u53e5\u7684\u6267\u884c\u987a\u5e8f\uff1a\u5230\u5e95\u8c01\u5148\u6267\u884c\uff1f\u63d2\u56fe\" alt=\"SQL\u4e2dselect\u4e0ehaving\u5b50\u53e5\u7684\u6267\u884c\u987a\u5e8f\uff1a\u5230\u5e95\u8c01\u5148\u6267\u884c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>sql\u4e2dselect\u4e0ehaving\u5b50\u53e5\u7684\u6267\u884c\u987a\u5e8f<\/strong><\/p>\n<p>\u4e2d\u5230\u5e95\u662f\u5148\u6267\u884cselect\u5b50\u53e5\u8fd8\u662fhaving\u5b50\u53e5\uff1f\u4ee5\u5982\u4e0b\u793a\u4f8b\u4ee3\u7801\u4e3a\u4f8b\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>\u4f20\u7edf\u7684\u8bf4\u6cd5\u662f\u5148\u6267\u884chaving\u5b50\u53e5\u540e\u6267\u884cselect\u5b50\u53e5\uff0c\u4f46\u8be5\u8bf4\u6cd5\u4f3c\u4e4e\u4e0e\u672c\u4f8b\u4e0d\u7b26\uff0c\u56e0\u4e3aselect\u5b50\u53e5\u4e2d\u5b9a\u4e49\u4e86&#8221;sum(num) as all_num&#8221;\uff0c\u4f3c\u4e4e\u5e94\u8be5\u5148\u6267\u884cselect\u5b50\u53e5\u624d\u80fd\u83b7\u53d6\u7528\u4e8ehaving\u6bd4\u8f83\u7684all_num\u3002 <\/p>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p>\u5b9e\u9645\u4e0a\uff0cselect\u5b50\u53e5\u5728\u8be5\u4f8b\u4e2d\u7684\u4f5c\u7528\u5e76\u4e0d\u662f\u64cd\u4f5c\u6570\u636e\uff0c\u800c\u662f\u5bf9having\u5b50\u53e5\u7b5b\u9009\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u9009\u62e9\u6027\u663e\u793a\u3002 sql\u8bed\u53e5\u7684\u6267\u884c\u987a\u5e8f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li> <strong>group by \u5206\u7ec4\uff1a<\/strong>\u6839\u636eid\u5217\u5c06\u6570\u636e\u5206\u7ec4\u3002 <\/li>\n<li> <strong>sum() \u8ba1\u7b97\uff1a<\/strong>\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u7684num\u7684\u603b\u548c\uff0c\u751f\u6210\u4e2d\u95f4\u7ed3\u679c\u3002 <\/li>\n<li> <strong>having \u5b50\u53e5\u7b5b\u9009\uff1a<\/strong>\u6839\u636eall_num\u8fc7\u6ee4\u4e2d\u95f4\u7ed3\u679c\uff0c\u4fdd\u7559\u603b\u548c\u5927\u4e8e50\u7684\u7ec4\u3002 <\/li>\n<li> <strong>select \u5b50\u53e5\u9009\u62e9\uff1a<\/strong>\u4ece\u6ee1\u8db3having\u6761\u4ef6\u7684\u7ec4\u4e2d\u9009\u62e9\u6027\u5730\u5c55\u793a\u7ed3\u679c\uff0c\u5373\u5c55\u793a\u6ee1\u8db3\u6761\u4ef6\u7684\u7ec4\u548c\u5bf9\u5e94\u7684all_num\u3002 <\/li>\n<\/ol>\n<p>\u56e0\u6b64\uff0cselect\u5b50\u53e5\u5e76\u975e\u5728having\u4e4b\u524d\u6267\u884c\uff0c\u800c\u662f\u5bf9\u5176\u7b5b\u9009\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u9009\u62e9\u6027\u663e\u793a\u3002\u6240\u4ee5\uff0c\u672c\u4f8b\u4e2d\u5148\u6267\u884chaving\u518d\u6267\u884cselect\u7684\u8bf4\u6cd5\u662f\u6b63\u786e\u7684\u3002 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSQL\u4e2dselect\u4e0ehaving\u5b50\u53e5\u7684\u6267\u884c\u987a\u5e8f\uff1a\u5230\u5e95\u8c01\u5148\u6267\u884c\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>sql\u4e2dselect\u4e0ehaving\u5b50\u53e5\u7684\u6267\u884c\u987a\u5e8f \u4e2d\u5230\u5e95\u662f\u5148\u6267\u884cselect\u5b50\u53e5\u8fd8\u662fhaving\u5b50\u53e5\uff1f\u4ee5\u5982\u4e0b\u793a\u4f8b\u4ee3\u7801\u4e3a\u4f8b\uff1a SELECT SUM(num) AS all_num FROM table GROUP BY id HAVING all_num &gt; 50; \u767b\u5f55\u540e\u590d\u5236 \u4f20\u7edf\u7684\u8bf4\u6cd5\u662f\u5148\u6267\u884chaving\u5b50\u53e5\u540e\u6267\u884cselect\u5b50\u53e5\uff0c\u4f46\u8be5\u8bf4\u6cd5\u4f3c\u4e4e\u4e0e\u672c\u4f8b\u4e0d\u7b26\uff0c\u56e0\u4e3aselect\u5b50\u53e5\u4e2d\u5b9a\u4e49\u4e86&#8221;sum(num) as all_num&#8221;\uff0c\u4f3c\u4e4e\u5e94\u8be5\u5148\u6267\u884cselect\u5b50\u53e5\u624d\u80fd\u83b7\u53d6\u7528\u4e8ehaving\u6bd4\u8f83\u7684all_num\u3002 \u89e3\u7b54\uff1a \u5b9e\u9645\u4e0a\uff0cselect\u5b50\u53e5\u5728\u8be5\u4f8b\u4e2d\u7684\u4f5c\u7528\u5e76\u4e0d\u662f\u64cd\u4f5c\u6570\u636e\uff0c\u800c\u662f\u5bf9having\u5b50\u53e5\u7b5b\u9009\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u9009\u62e9\u6027\u663e\u793a\u3002 sql\u8bed\u53e5\u7684\u6267\u884c\u987a\u5e8f\u5982\u4e0b\uff1a group by \u5206\u7ec4\uff1a\u6839\u636eid\u5217\u5c06\u6570\u636e\u5206\u7ec4\u3002 sum() \u8ba1\u7b97\uff1a\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u7684num\u7684\u603b\u548c\uff0c\u751f\u6210\u4e2d\u95f4\u7ed3\u679c\u3002 having \u5b50\u53e5\u7b5b\u9009\uff1a\u6839\u636eall_num\u8fc7\u6ee4\u4e2d\u95f4\u7ed3\u679c\uff0c\u4fdd\u7559\u603b\u548c\u5927\u4e8e50\u7684\u7ec4\u3002 select \u5b50\u53e5\u9009\u62e9\uff1a\u4ece\u6ee1\u8db3having\u6761\u4ef6\u7684\u7ec4\u4e2d\u9009\u62e9\u6027\u5730\u5c55\u793a\u7ed3\u679c\uff0c\u5373\u5c55\u793a\u6ee1\u8db3\u6761\u4ef6\u7684\u7ec4\u548c\u5bf9\u5e94\u7684all_num\u3002 \u56e0\u6b64\uff0cselect\u5b50\u53e5\u5e76\u975e\u5728having\u4e4b\u524d\u6267\u884c\uff0c\u800c\u662f\u5bf9\u5176\u7b5b\u9009\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u9009\u62e9\u6027\u663e\u793a\u3002\u6240\u4ee5\uff0c\u672c\u4f8b\u4e2d\u5148\u6267\u884chaving\u518d\u6267\u884cselect\u7684\u8bf4\u6cd5\u662f\u6b63\u786e\u7684\u3002 \u4ee5\u4e0a\u5c31\u662fSQL\u4e2dselect\u4e0ehaving\u5b50\u53e5\u7684\u6267\u884c\u987a\u5e8f\uff1a\u5230\u5e95\u8c01\u5148\u6267\u884c\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},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-7941","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7941","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=7941"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7941\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}