{"id":5623,"date":"2024-11-14T09:12:17","date_gmt":"2024-11-14T01:12:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5623\/"},"modified":"2024-11-14T09:12:17","modified_gmt":"2024-11-14T01:12:17","slug":"sql%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5623\/","title":{"rendered":"sql\u5d4c\u5957\u67e5\u8be2\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  SQL\u5d4c\u5957\u67e5\u8be2\u5c06\u4e00\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u67e5\u8be2\u7684\u8f93\u5165\uff0c\u521b\u5efa\u66f4\u590d\u6742\u7684\u67e5\u8be2\u3002\u5d4c\u5957\u67e5\u8be2\u4f7f\u7528\u5b50\u67e5\u8be2\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a\u7f16\u5199\u5b50\u67e5\u8be2\uff0c\u4ece\u8868\u4e2d\u9009\u62e9\u5217\u548c\u8bb0\u5f55\uff0c\u4f7f\u7528WHERE\u5b50\u53e5\u8fc7\u6ee4\u6570\u636e\u3002\u5c06\u5b50\u67e5\u8be2\u62ec\u5728\u5706\u62ec\u53f7\u4e2d\uff0c\u4f5c\u4e3a\u5916\u90e8\u67e5\u8be2\u7684\u6bd4\u8f83\u6216\u96c6\u5408\u64cd\u4f5c\u7684\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u9500\u552e\u989d\u8d85\u8fc7$1000\u7684\u5ba2\u6237\u59d3\u540d\u548c\u9500\u552e\u989d\uff1aSELECT customer_name, SUM(sales_amount) AS total_sales FROM customers WHERE customer_id IN ( SELECT customer_id FR\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053019125772007.jpg\" class=\"aligncenter\" title=\"sql\u5d4c\u5957\u67e5\u8be2\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"sql\u5d4c\u5957\u67e5\u8be2\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u5d4c\u5957\u67e5\u8be2<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f SQL \u5d4c\u5957\u67e5\u8be2\uff1f<\/strong><\/p>\n<p>\u5d4c\u5957\u67e5\u8be2\u662f\u5c06\u4e00\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u67e5\u8be2\u7684\u8f93\u5165\uff0c\u4ece\u800c\u521b\u5efa\u66f4\u590d\u6742\u548c\u5f3a\u5927\u7684\u67e5\u8be2\u3002<\/p>\n<p><strong>\u5982\u4f55\u7f16\u5199 SQL \u5d4c\u5957\u67e5\u8be2\uff1f<\/strong><\/p>\n<p>\u5d4c\u5957\u67e5\u8be2\u901a\u5e38\u4f7f\u7528\u5b50\u67e5\u8be2\uff0c\u8fd9\u662f\u4e00\u79cd\u5305\u542b\u5728\u53e6\u4e00\u4e2a\u67e5\u8be2\u5185\u90e8\u7684\u67e5\u8be2\u3002\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u4f5c\u4e3a\u5916\u90e8\u67e5\u8be2\u4e2d\u67d0\u4e2a\u64cd\u4f5c\u7684\u8f93\u5165\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u822c\u8bed\u6cd5\uff1a<\/p>\n<pre>EXTERNAL_QUERY\nWHERE (SUB_QUERY)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\n<p>\u7f16\u5199\u5b50\u67e5\u8be2\uff1a<\/p>\n<ul>\n<li>\u4ece\u4e00\u4e2a\u6216\u591a\u4e2a\u8868\u4e2d\u9009\u62e9\u9700\u8981\u7684\u5217\u548c\u8bb0\u5f55\u3002<\/li>\n<li>\u4f7f\u7528 WHERE \u5b50\u53e5\u8fc7\u6ee4\u6570\u636e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5c06\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u4f5c\u4e3a\u5916\u90e8\u67e5\u8be2\u7684\u8f93\u5165\uff1a<\/p>\n<ul>\n<li>\u5c06\u5b50\u67e5\u8be2\u62ec\u5728\u5706\u62ec\u53f7\u4e2d\u3002<\/li>\n<li>\u5c06\u5b50\u67e5\u8be2\u4f5c\u4e3a\u5916\u90e8\u67e5\u8be2\u4e2d\u6bd4\u8f83\u64cd\u4f5c\u6216\u96c6\u5408\u64cd\u4f5c\u7684\u8f93\u5165\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u5d4c\u5957\u67e5\u8be2\u83b7\u53d6\u9500\u552e\u989d\u8d85\u8fc7 1000 \u7f8e\u5143\u7684\u5ba2\u6237\u7684\u59d3\u540d\u548c\u9500\u552e\u989d\uff1a<\/p>\n<pre>SELECT customer_name, SUM(sales_amount) AS total_sales\nFROM customers\nWHERE customer_id IN (\n    SELECT customer_id\n    FROM sales\n    WHERE sales_amount &gt; 1000\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u5b50\u67e5\u8be2\u53ef\u4ee5\u5d4c\u5957\u5728\u591a\u4e2a\u7ea7\u522b\u3002<\/li>\n<li>\u5916\u90e8\u67e5\u8be2\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5b50\u67e5\u8be2\u3002<\/li>\n<li>\u5b50\u67e5\u8be2\u53ef\u4ee5\u4f7f\u7528\u76f8\u5173\u8054\u7684\u5217\u6216\u4f7f\u7528\u5d4c\u5957\u8fde\u63a5\u6765\u5f15\u7528\u5916\u90e8\u67e5\u8be2\u7684\u8868\u3002<\/li>\n<li>\u786e\u4fdd\u5b50\u67e5\u8be2\u8fd4\u56de\u4e0e\u5916\u90e8\u67e5\u8be2\u4e2d\u9884\u671f\u7c7b\u578b\u548c\u6570\u91cf\u76f8\u5339\u914d\u7684\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u5d4c\u5957\u67e5\u8be2\u600e\u4e48\u5199\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\u5d4c\u5957\u67e5\u8be2\u5c06\u4e00\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u67e5\u8be2\u7684\u8f93\u5165\uff0c\u521b\u5efa\u66f4\u590d\u6742\u7684\u67e5\u8be2\u3002\u5d4c\u5957\u67e5\u8be2\u4f7f\u7528\u5b50\u67e5\u8be2\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a\u7f16\u5199\u5b50\u67e5\u8be2\uff0c\u4ece\u8868\u4e2d\u9009\u62e9\u5217\u548c\u8bb0\u5f55\uff0c\u4f7f\u7528WHERE\u5b50\u53e5\u8fc7\u6ee4\u6570\u636e\u3002\u5c06\u5b50\u67e5\u8be2\u62ec\u5728\u5706\u62ec\u53f7\u4e2d\uff0c\u4f5c\u4e3a\u5916\u90e8\u67e5\u8be2\u7684\u6bd4\u8f83\u6216\u96c6\u5408\u64cd\u4f5c\u7684\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u9500\u552e\u989d\u8d85\u8fc7$1000\u7684\u5ba2\u6237\u59d3\u540d\u548c\u9500\u552e\u989d\uff1aSELECT customer_name, SUM(sales_amount) AS total_sales FROM customers WHERE customer_id IN ( SELECT customer_id FR SQL \u5d4c\u5957\u67e5\u8be2 \u4ec0\u4e48\u662f SQL \u5d4c\u5957\u67e5\u8be2\uff1f \u5d4c\u5957\u67e5\u8be2\u662f\u5c06\u4e00\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u67e5\u8be2\u7684\u8f93\u5165\uff0c\u4ece\u800c\u521b\u5efa\u66f4\u590d\u6742\u548c\u5f3a\u5927\u7684\u67e5\u8be2\u3002 \u5982\u4f55\u7f16\u5199 SQL \u5d4c\u5957\u67e5\u8be2\uff1f \u5d4c\u5957\u67e5\u8be2\u901a\u5e38\u4f7f\u7528\u5b50\u67e5\u8be2\uff0c\u8fd9\u662f\u4e00\u79cd\u5305\u542b\u5728\u53e6\u4e00\u4e2a\u67e5\u8be2\u5185\u90e8\u7684\u67e5\u8be2\u3002\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u4f5c\u4e3a\u5916\u90e8\u67e5\u8be2\u4e2d\u67d0\u4e2a\u64cd\u4f5c\u7684\u8f93\u5165\u3002 \u4ee5\u4e0b\u662f\u4e00\u822c\u8bed\u6cd5\uff1a EXTERNAL_QUERY WHERE (SUB_QUERY) \u767b\u5f55\u540e\u590d\u5236 \u6b65\u9aa4\uff1a \u7f16\u5199\u5b50\u67e5\u8be2\uff1a \u4ece\u4e00\u4e2a\u6216\u591a\u4e2a\u8868\u4e2d\u9009\u62e9\u9700\u8981\u7684\u5217\u548c\u8bb0\u5f55\u3002 \u4f7f\u7528 WHERE \u5b50\u53e5\u8fc7\u6ee4\u6570\u636e\u3002 \u5c06\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u4f5c\u4e3a\u5916\u90e8\u67e5\u8be2\u7684\u8f93\u5165\uff1a \u5c06\u5b50\u67e5\u8be2\u62ec\u5728\u5706\u62ec\u53f7\u4e2d\u3002 \u5c06\u5b50\u67e5\u8be2\u4f5c\u4e3a\u5916\u90e8\u67e5\u8be2\u4e2d\u6bd4\u8f83\u64cd\u4f5c\u6216\u96c6\u5408\u64cd\u4f5c\u7684\u8f93\u5165\u3002 \u793a\u4f8b\uff1a \u4ee5\u4e0b\u5d4c\u5957\u67e5\u8be2\u83b7\u53d6\u9500\u552e\u989d\u8d85\u8fc7 1000 \u7f8e\u5143\u7684\u5ba2\u6237\u7684\u59d3\u540d\u548c\u9500\u552e\u989d\uff1a SELECT customer_name, SUM(sales_amount) AS total_sales FROM customers WHERE customer_id IN ( SELECT customer_id [&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-5623","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5623","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=5623"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5623\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}