{"id":2648,"date":"2025-03-11T23:55:05","date_gmt":"2025-03-11T15:55:05","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2648"},"modified":"2025-03-11T23:55:05","modified_gmt":"2025-03-11T15:55:05","slug":"postgresql-having-%e5%ad%90%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/11\/postgresql-having-%e5%ad%90%e5%8f%a5\/","title":{"rendered":"PostgreSQL HAVING \u5b50\u53e5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 PostgreSQL \u4e2d\uff0c<code>HAVING<\/code> \u5b50\u53e5\u7528\u4e8e\u5728 <code>GROUP BY<\/code> \u67e5\u8be2\u4e2d\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u8fc7\u6ee4\u3002\u4e0e <code>WHERE<\/code> \u5b50\u53e5\u4e0d\u540c\uff0c<code>WHERE<\/code> \u5b50\u53e5\u662f\u5bf9\u539f\u59cb\u6570\u636e\u8fdb\u884c\u8fc7\u6ee4\uff0c\u800c <code>HAVING<\/code> \u5b50\u53e5\u662f\u5728\u5206\u7ec4\u4e4b\u540e\u5bf9\u805a\u5408\u7ed3\u679c\u8fdb\u884c\u8fc7\u6ee4\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong><code>HAVING<\/code> \u5b50\u53e5\u7684\u57fa\u672c\u8bed\u6cd5<\/strong><\/h2>\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><strong><code>aggregate_function(column3)<\/code><\/strong>: \u805a\u5408\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u67d0\u4e2a\u5217\u8fdb\u884c\u8ba1\u7b97\uff08\u5982 <code>SUM()<\/code>\u3001<code>AVG()<\/code>\u3001<code>COUNT()<\/code>\u3001<code>MAX()<\/code> \u7b49\uff09\u3002<\/li>\n\n\n\n<li><strong><code>HAVING<\/code><\/strong>: \u7528\u6765\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u8fc7\u6ee4\uff0c\u901a\u5e38\u4e0e\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong><code>HAVING<\/code> \u5b50\u53e5\u7684\u57fa\u672c\u793a\u4f8b<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a <code>sales<\/code> \u7684\u8868\uff0c\u5305\u542b <code>product_id<\/code>\u3001<code>amount<\/code> \u548c <code>sale_date<\/code> \u7b49\u5b57\u6bb5\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f7f\u7528 <code>HAVING<\/code> \u5b50\u53e5\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 <strong>\u8fc7\u6ee4\u5206\u7ec4\u540e\u7684\u7ed3\u679c<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u83b7\u53d6\u6bcf\u4e2a\u4ea7\u54c1\u7684\u603b\u9500\u552e\u989d\uff0c\u4f46\u53ea\u9009\u62e9\u603b\u9500\u552e\u989d\u5927\u4e8e 1000 \u7684\u4ea7\u54c1\nSELECT product_id, SUM(amount) AS total_sales\nFROM sales\nGROUP BY product_id\nHAVING SUM(amount) &gt; 1000;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>HAVING<\/code> \u5b50\u53e5\u7528\u4e8e\u8fc7\u6ee4\u603b\u9500\u552e\u989d\u5927\u4e8e 1000 \u7684\u4ea7\u54c1\u3002\u6ce8\u610f\uff0c<code>HAVING<\/code> \u5b50\u53e5\u662f\u5728 <code>GROUP BY<\/code> \u4e4b\u540e\u5e94\u7528\u7684\uff0c\u56e0\u6b64\u53ea\u80fd\u7528\u4e8e\u805a\u5408\u51fd\u6570\u7684\u6761\u4ef6\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. <strong><code>HAVING<\/code> \u4e0e <code>WHERE<\/code> \u7684\u533a\u522b<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>WHERE<\/code> \u5b50\u53e5<\/strong>: \u7528\u4e8e\u5728\u5206\u7ec4\u4e4b\u524d\u8fc7\u6ee4\u539f\u59cb\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong><code>HAVING<\/code> \u5b50\u53e5<\/strong>: \u7528\u4e8e\u5728 <code>GROUP BY<\/code> \u4e4b\u540e\u8fc7\u6ee4\u5206\u7ec4\u7ed3\u679c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 <strong>\u4f7f\u7528 <code>WHERE<\/code> \u548c <code>HAVING<\/code> \u5b50\u53e5\u7684\u4f8b\u5b50<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5148\u7b5b\u9009\u51fa\u9500\u552e\u91d1\u989d\u5927\u4e8e 100 \u7684\u8bb0\u5f55\uff0c\u7136\u540e\u6309\u4ea7\u54c1\u5206\u7ec4\u5e76\u8fc7\u6ee4\u603b\u9500\u552e\u989d\u5927\u4e8e 1000 \u7684\u4ea7\u54c1\nSELECT product_id, SUM(amount) AS total_sales\nFROM sales\nWHERE amount &gt; 100\nGROUP BY product_id\nHAVING SUM(amount) &gt; 1000;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>WHERE<\/code> \u5b50\u53e5\u9996\u5148\u7b5b\u9009\u51fa <code>amount<\/code> \u5927\u4e8e 100 \u7684\u9500\u552e\u8bb0\u5f55\u3002\u7136\u540e\uff0c<code>GROUP BY<\/code> \u6309 <code>product_id<\/code> \u5206\u7ec4\uff0c\u6700\u540e <code>HAVING<\/code> \u5b50\u53e5\u8fc7\u6ee4\u51fa\u603b\u9500\u552e\u989d\u5927\u4e8e 1000 \u7684\u4ea7\u54c1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. <strong>\u591a\u4e2a\u6761\u4ef6\u7684 <code>HAVING<\/code> \u5b50\u53e5<\/strong><\/h2>\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\uff0c\u5e76\u4e14\u8fd9\u4e9b\u6761\u4ef6\u53ef\u4ee5\u901a\u8fc7 <code>AND<\/code> \u6216 <code>OR<\/code> \u8fde\u63a5\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u83b7\u53d6\u6bcf\u4e2a\u4ea7\u54c1\u7684\u603b\u9500\u552e\u989d\uff0c\u5e76\u7b5b\u9009\u51fa\u603b\u9500\u552e\u989d\u5927\u4e8e 1000 \u4e14\u9500\u552e\u8bb0\u5f55\u6570\u5927\u4e8e 10 \u7684\u4ea7\u54c1\nSELECT product_id, SUM(amount) AS total_sales, COUNT(*) AS sales_count\nFROM sales\nGROUP BY product_id\nHAVING SUM(amount) &gt; 1000 AND COUNT(*) &gt; 10;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>HAVING<\/code> \u5b50\u53e5\u540c\u65f6\u5bf9 <code>SUM(amount)<\/code> \u548c <code>COUNT(*)<\/code> \u8fdb\u884c\u8fc7\u6ee4\uff0c\u786e\u4fdd\u8fd4\u56de\u7684\u4ea7\u54c1\u7b26\u5408\u4e24\u4e2a\u6761\u4ef6\uff1a\u603b\u9500\u552e\u989d\u5927\u4e8e 1000 \u4e14\u9500\u552e\u8bb0\u5f55\u6570\u5927\u4e8e 10\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. <strong><code>HAVING<\/code> \u5b50\u53e5\u4e0e\u805a\u5408\u51fd\u6570<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>HAVING<\/code> \u5b50\u53e5\u6700\u5e38\u4e0e\u805a\u5408\u51fd\u6570\uff08\u5982 <code>COUNT()<\/code>\u3001<code>SUM()<\/code>\u3001<code>AVG()<\/code>\u3001<code>MAX()<\/code>\u3001<code>MIN()<\/code>\uff09\u4e00\u8d77\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7528\u6cd5\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.1 <strong>\u4f7f\u7528 <code>COUNT()<\/code> \u805a\u5408\u51fd\u6570<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u83b7\u53d6\u9500\u552e\u8bb0\u5f55\u6570\u5927\u4e8e 50 \u7684\u4ea7\u54c1\nSELECT product_id, COUNT(*) AS sales_count\nFROM sales\nGROUP BY product_id\nHAVING COUNT(*) &gt; 50;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5.2 <strong>\u4f7f\u7528 <code>AVG()<\/code> \u805a\u5408\u51fd\u6570<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u83b7\u53d6\u5e73\u5747\u9500\u552e\u989d\u5927\u4e8e 200 \u7684\u4ea7\u54c1\nSELECT product_id, AVG(amount) AS average_sales\nFROM sales\nGROUP BY product_id\nHAVING AVG(amount) &gt; 200;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5.3 <strong>\u4f7f\u7528 <code>MAX()<\/code> \u805a\u5408\u51fd\u6570<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u83b7\u53d6\u6700\u5927\u9500\u552e\u989d\u5927\u4e8e 1000 \u7684\u4ea7\u54c1\nSELECT product_id, MAX(amount) AS max_sales\nFROM sales\nGROUP BY product_id\nHAVING MAX(amount) > 1000;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. <strong>\u4e0e <code>ORDER BY<\/code> \u5b50\u53e5\u7ed3\u5408\u4f7f\u7528<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>HAVING<\/code> \u5b50\u53e5\u901a\u5e38\u4e0e <code>GROUP BY<\/code> \u4e00\u8d77\u4f7f\u7528\uff0c\u4f46\u4e5f\u53ef\u4ee5\u4e0e <code>ORDER BY<\/code> \u5b50\u53e5\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5bf9\u5206\u7ec4\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u83b7\u53d6\u6bcf\u4e2a\u4ea7\u54c1\u7684\u603b\u9500\u552e\u989d\uff0c\u5e76\u6309\u603b\u9500\u552e\u989d\u964d\u5e8f\u6392\u5e8f\nSELECT product_id, SUM(amount) AS total_sales\nFROM sales\nGROUP BY product_id\nHAVING SUM(amount) &gt; 1000\nORDER BY total_sales DESC;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>HAVING<\/code> \u5b50\u53e5\u9996\u5148\u8fc7\u6ee4\u51fa\u603b\u9500\u552e\u989d\u5927\u4e8e 1000 \u7684\u4ea7\u54c1\uff0c\u63a5\u7740\u4f7f\u7528 <code>ORDER BY<\/code> \u6309 <code>total_sales<\/code> \u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. <strong>\u6027\u80fd\u8003\u8651<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u4f7f\u7528 <code>HAVING<\/code> \u5b50\u53e5\u65f6\uff0cPostgreSQL \u4f1a\u5148\u6267\u884c <code>GROUP BY<\/code> \u64cd\u4f5c\uff0c\u7136\u540e\u518d\u5e94\u7528 <code>HAVING<\/code> \u6761\u4ef6\u8fc7\u6ee4\u5206\u7ec4\u7ed3\u679c\u3002\u56e0\u6b64\uff0c<code>HAVING<\/code> \u5b50\u53e5\u901a\u5e38\u5728\u805a\u5408\u64cd\u4f5c\u540e\u5904\u7406\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u6761\u4ef6\u80fd\u5728 <code>GROUP BY<\/code> \u4e4b\u524d\u5e94\u7528\uff0c\u5c3d\u91cf\u4f7f\u7528 <code>WHERE<\/code> \u5b50\u53e5\u8fdb\u884c\u8fc7\u6ee4\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5206\u7ec4\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">8. <strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>HAVING<\/code> \u5b50\u53e5<\/strong> \u7528\u4e8e\u8fc7\u6ee4\u5206\u7ec4\u540e\u7684\u6570\u636e\uff0c\u901a\u5e38\u4e0e <code>GROUP BY<\/code> \u548c\u805a\u5408\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong><code>WHERE<\/code> \u5b50\u53e5<\/strong> \u7528\u4e8e\u5728\u5206\u7ec4\u524d\u8fc7\u6ee4\u539f\u59cb\u6570\u636e\uff0c\u800c <strong><code>HAVING<\/code> \u5b50\u53e5<\/strong> \u7528\u4e8e\u5728\u5206\u7ec4\u540e\u8fc7\u6ee4\u805a\u5408\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><code>HAVING<\/code> \u53ef\u4ee5\u5904\u7406\u591a\u4e2a\u6761\u4ef6\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e0e <code>AND<\/code> \u548c <code>OR<\/code> \u8fde\u63a5\u3002<\/li>\n\n\n\n<li><code>HAVING<\/code> \u5b50\u53e5\u5e38\u5e38\u4e0e\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u5982 <code>COUNT()<\/code>\u3001<code>SUM()<\/code>\u3001<code>AVG()<\/code>\u3001<code>MAX()<\/code> \u548c <code>MIN()<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>HAVING<\/code> \u548c <code>ORDER BY<\/code><\/strong> \u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\uff0c\u5148\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u518d\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4f7f\u7528 <code>HAVING<\/code> \u5b50\u53e5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7b5b\u9009\u51fa\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u5206\u7ec4\u7ed3\u679c\uff0c\u4f7f\u5f97\u67e5\u8be2\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PostgreSQL \u4e2d\uff0cHAVING \u5b50\u53e5\u7528\u4e8e\u5728 GROUP BY \u67e5\u8be2\u4e2d\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u8fc7\u6ee4\u3002\u4e0e W [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2649,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-2648","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2648","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=2648"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2648\/revisions"}],"predecessor-version":[{"id":2650,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2648\/revisions\/2650"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2649"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}