{"id":1731,"date":"2025-02-16T09:03:48","date_gmt":"2025-02-16T01:03:48","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1731"},"modified":"2025-02-16T09:03:48","modified_gmt":"2025-02-16T01:03:48","slug":"sql-having%e5%ad%90%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/16\/sql-having%e5%ad%90%e5%8f%a5\/","title":{"rendered":"SQL HAVING\u5b50\u53e5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>HAVING<\/code> \u662f SQL \u4e2d\u4e00\u4e2a\u91cd\u8981\u7684\u5b50\u53e5\uff0c\u7528\u4e8e\u5728 <code>GROUP BY<\/code> \u5206\u7ec4\u540e\u7b5b\u9009\u6570\u636e\u3002\u5b83\u4e0e <code>WHERE<\/code> \u5b50\u53e5\u4e0d\u540c\uff0c<code>WHERE<\/code> \u7528\u4e8e\u7b5b\u9009\u884c\u6570\u636e\uff0c\u800c <code>HAVING<\/code> \u7528\u4e8e\u7b5b\u9009\u805a\u5408\u7ed3\u679c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong><code>HAVING<\/code> \u7684\u57fa\u672c\u7528\u6cd5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>HAVING<\/code> \u5b50\u53e5\u901a\u5e38\u4e0e <code>GROUP BY<\/code> \u5b50\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u7528\u4e8e\u5bf9\u5206\u7ec4\u540e\u7684\u6570\u636e\u8fdb\u884c\u8fc7\u6ee4\u3002\u5b83\u901a\u5e38\u4e0e\u805a\u5408\u51fd\u6570\uff08\u5982 <code>SUM()<\/code>\u3001<code>AVG()<\/code>\u3001<code>COUNT()<\/code>\u3001<code>MAX()<\/code> \u7b49\uff09\u4e00\u8d77\u4f7f\u7528\uff0c\u7528\u4e8e\u7b5b\u9009\u7b26\u5408\u6761\u4ef6\u7684\u5206\u7ec4\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bed\u6cd5\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2, aggregate_function(column3)\nFROM table_name\nWHERE condition\nGROUP BY column1, column2\nHAVING aggregate_function(column3) condition;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>column1, column2<\/code>\uff1a\u4f60\u60f3\u8981\u5206\u7ec4\u7684\u5217\u3002<\/li>\n\n\n\n<li><code>aggregate_function(column3)<\/code>\uff1a\u4f60\u5e0c\u671b\u5e94\u7528\u7684\u805a\u5408\u51fd\u6570\uff0c\u5982 <code>SUM()<\/code>\u3001<code>COUNT()<\/code>\u3001<code>AVG()<\/code> \u7b49\u3002<\/li>\n\n\n\n<li><code>HAVING<\/code> \u5b50\u53e5\u540e\u9762\u8ddf\u805a\u5408\u51fd\u6570\u7684\u6761\u4ef6\uff0c\u7528\u4e8e\u7b5b\u9009\u5206\u7ec4\u540e\u7684\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>HAVING<\/code> \u5b50\u53e5\u7684\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a <code>sales<\/code> \u7684\u8868\uff0c\u5305\u542b\u4ee5\u4e0b\u6570\u636e\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>sale_id<\/th><th>product_id<\/th><th>quantity<\/th><th>price<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>101<\/td><td>10<\/td><td>5<\/td><\/tr><tr><td>2<\/td><td>102<\/td><td>5<\/td><td>8<\/td><\/tr><tr><td>3<\/td><td>101<\/td><td>7<\/td><td>5<\/td><\/tr><tr><td>4<\/td><td>103<\/td><td>3<\/td><td>10<\/td><\/tr><tr><td>5<\/td><td>102<\/td><td>12<\/td><td>8<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>\u6309\u6761\u4ef6\u8fc7\u6ee4\u5206\u7ec4<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u8be2\u6bcf\u79cd\u4ea7\u54c1\u7684\u9500\u552e\u603b\u6570\u91cf\uff0c\u4f46\u4ec5\u8fd4\u56de\u9500\u552e\u603b\u6570\u91cf\u5927\u4e8e 10 \u7684\u4ea7\u54c1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT product_id, SUM(quantity) AS total_quantity\nFROM sales\nGROUP BY product_id\nHAVING SUM(quantity) &gt; 10;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>product_id<\/th><th>total_quantity<\/th><\/tr><\/thead><tbody><tr><td>101<\/td><td>17<\/td><\/tr><tr><td>102<\/td><td>17<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong><code>HAVING<\/code> \u548c <code>WHERE<\/code> \u7684\u7ed3\u5408\u4f7f\u7528<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>WHERE<\/code> \u5b50\u53e5\u7528\u4e8e\u7b5b\u9009\u539f\u59cb\u6570\u636e\uff0c\u800c <code>HAVING<\/code> \u5b50\u53e5\u7528\u4e8e\u7b5b\u9009\u5206\u7ec4\u540e\u7684\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u7528 <code>WHERE<\/code> \u7b5b\u9009\u6570\u636e\uff0c\u7136\u540e\u7528 <code>HAVING<\/code> \u5bf9\u5206\u7ec4\u7ed3\u679c\u8fdb\u884c\u8fc7\u6ee4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u67e5\u8be2\u9500\u552e\u6570\u91cf\u5927\u4e8e 5 \u4e14\u9500\u552e\u603b\u91d1\u989d\u5927\u4e8e 50 \u7684\u4ea7\u54c1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT product_id, SUM(quantity * price) AS total_sales\nFROM sales\nWHERE quantity &gt; 5\nGROUP BY product_id\nHAVING SUM(quantity * price) &gt; 50;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>product_id<\/th><th>total_sales<\/th><\/tr><\/thead><tbody><tr><td>101<\/td><td>85<\/td><\/tr><tr><td>102<\/td><td>136<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6b64\u67e5\u8be2\u4e2d\uff0c<code>WHERE<\/code> \u7b5b\u9009\u4e86\u6570\u91cf\u5927\u4e8e 5 \u7684\u8bb0\u5f55\uff0c<code>HAVING<\/code> \u7b5b\u9009\u4e86\u9500\u552e\u603b\u91d1\u989d\u5927\u4e8e 50 \u7684\u5206\u7ec4\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.3 <strong>\u591a\u4e2a\u6761\u4ef6\u8fc7\u6ee4<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u5728 <code>HAVING<\/code> \u5b50\u53e5\u4e2d\u4f7f\u7528\u591a\u4e2a\u6761\u4ef6\u8fdb\u884c\u7b5b\u9009\u3002\u4f8b\u5982\uff0c\u67e5\u8be2\u9500\u552e\u603b\u6570\u91cf\u5927\u4e8e 10 \u4e14\u9500\u552e\u603b\u91d1\u989d\u5927\u4e8e 50 \u7684\u4ea7\u54c1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT product_id, SUM(quantity) AS total_quantity, SUM(quantity * price) AS total_sales\nFROM sales\nGROUP BY product_id\nHAVING SUM(quantity) &gt; 10 AND SUM(quantity * price) &gt; 50;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>product_id<\/th><th>total_quantity<\/th><th>total_sales<\/th><\/tr><\/thead><tbody><tr><td>101<\/td><td>17<\/td><td>85<\/td><\/tr><tr><td>102<\/td><td>17<\/td><td>136<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong><code>HAVING<\/code> \u5b50\u53e5\u7684\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 <strong><code>HAVING<\/code> \u4ec5\u7528\u4e8e\u805a\u5408\u6570\u636e<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>HAVING<\/code> \u53ea\u5728\u5206\u7ec4\u540e\u5e94\u7528\uff0c\u4e0d\u80fd\u50cf <code>WHERE<\/code> \u4e00\u6837\u76f4\u63a5\u8fc7\u6ee4\u539f\u59cb\u6570\u636e\u3002\u56e0\u6b64\uff0c<code>HAVING<\/code> \u5fc5\u987b\u4e0e\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u800c\u4e0d\u80fd\u76f4\u63a5\u5f15\u7528\u975e\u805a\u5408\u5217\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u4f1a\u629b\u51fa\u9519\u8bef\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT product_id, SUM(quantity)\nFROM sales\nGROUP BY product_id\nHAVING product_id = 101;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u9519\u8bef\u539f\u56e0<\/strong>\uff1a<code>HAVING<\/code> \u7528\u4e8e\u8fc7\u6ee4\u805a\u5408\u7ed3\u679c\uff0c\u800c\u4e0d\u80fd\u7528\u4e8e\u76f4\u63a5\u8fc7\u6ee4 <code>product_id<\/code>\uff08\u5b83\u662f\u4e00\u4e2a\u539f\u59cb\u5217\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a\u4f60\u53ef\u4ee5\u5c06\u6761\u4ef6\u653e\u5728 <code>WHERE<\/code> \u5b50\u53e5\u4e2d\uff0c\u6216\u8005\u5728 <code>HAVING<\/code> \u4e2d\u4f7f\u7528\u805a\u5408\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT product_id, SUM(quantity)\nFROM sales\nWHERE product_id = 101\nGROUP BY product_id;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 <strong><code>WHERE<\/code> \u548c <code>HAVING<\/code> \u7684\u6df7\u6dc6<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>WHERE<\/code> \u7528\u4e8e\u5728\u6570\u636e\u5206\u7ec4\u524d\u8fc7\u6ee4\u539f\u59cb\u6570\u636e\uff0c\u800c <code>HAVING<\/code> \u7528\u4e8e\u8fc7\u6ee4\u5206\u7ec4\u540e\u7684\u6570\u636e\u3002\u4f60\u4e0d\u80fd\u5c06\u5b83\u4eec\u4e92\u6362\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u9519\u8bef\u7528\u6cd5<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT product_id, SUM(quantity)\nFROM sales\nHAVING quantity &gt; 5\nGROUP BY product_id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong>\uff1a\u5e94\u8be5\u4f7f\u7528 <code>WHERE<\/code> \u6765\u7b5b\u9009\u539f\u59cb\u6570\u636e\uff0c<code>HAVING<\/code> \u7528\u4e8e\u7b5b\u9009\u5206\u7ec4\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT product_id, SUM(quantity)\nFROM sales\nWHERE quantity &gt; 5\nGROUP BY product_id;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.3 <strong>\u6027\u80fd\u95ee\u9898<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>HAVING<\/code> \u5b50\u53e5\u53ef\u80fd\u4f1a\u5f71\u54cd\u67e5\u8be2\u6027\u80fd\uff0c\u7279\u522b\u662f\u5f53\u6570\u636e\u96c6\u975e\u5e38\u5e9e\u5927\u65f6\u3002\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u5efa\u8bae\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>WHERE<\/code> \u5b50\u53e5\u63d0\u524d\u7b5b\u9009\u6570\u636e\uff0c\u5c3d\u91cf\u51cf\u5c11 <code>HAVING<\/code> \u7684\u4f7f\u7528\u3002<\/li>\n\n\n\n<li>\u786e\u4fdd\u5728\u6d89\u53ca\u7684\u5217\u4e0a\u6709\u5408\u9002\u7684\u7d22\u5f15\uff0c\u7279\u522b\u662f\u7528\u4e8e\u5206\u7ec4\u7684\u5217\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>HAVING<\/code> \u7528\u4e8e\u8fc7\u6ee4 <code>GROUP BY<\/code> \u5206\u7ec4\u540e\u7684\u7ed3\u679c\uff0c\u901a\u5e38\u4e0e\u805a\u5408\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><code>WHERE<\/code> \u7528\u4e8e\u8fc7\u6ee4\u539f\u59cb\u6570\u636e\u884c\uff0c\u800c <code>HAVING<\/code> \u7528\u4e8e\u8fc7\u6ee4\u805a\u5408\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><code>HAVING<\/code> \u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u6761\u4ef6\u548c\u590d\u6742\u7684\u805a\u5408\u903b\u8f91\uff0c\u4f46\u5fc5\u987b\u7528\u4e8e\u805a\u5408\u51fd\u6570\u7684\u7ed3\u679c\u3002<\/li>\n\n\n\n<li>\u786e\u4fdd\u7406\u89e3 <code>WHERE<\/code> \u548c <code>HAVING<\/code> \u7684\u533a\u522b\uff0c\u5408\u7406\u4f7f\u7528\u5b83\u4eec\uff0c\u4ee5\u907f\u514d\u9519\u8bef\u5e76\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>HAVING \u662f SQL \u4e2d\u4e00\u4e2a\u91cd\u8981\u7684\u5b50\u53e5\uff0c\u7528\u4e8e\u5728 GROUP BY \u5206\u7ec4\u540e\u7b5b\u9009\u6570\u636e\u3002\u5b83\u4e0e WHERE \u5b50\u53e5\u4e0d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-1731","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1731"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1731\/revisions"}],"predecessor-version":[{"id":1732,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1731\/revisions\/1732"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}