{"id":1510,"date":"2025-02-10T23:34:11","date_gmt":"2025-02-10T15:34:11","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1510"},"modified":"2025-02-10T23:34:12","modified_gmt":"2025-02-10T15:34:12","slug":"%e9%ab%98%e7%ba%a7sql%e6%9f%a5%e8%af%a2%e6%8a%80%e5%b7%a7%ef%bc%9ajoin%e3%80%81%e5%ad%90%e6%9f%a5%e8%af%a2%e4%b8%8e%e8%81%94%e5%90%88%e6%9f%a5%e8%af%a2%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/10\/%e9%ab%98%e7%ba%a7sql%e6%9f%a5%e8%af%a2%e6%8a%80%e5%b7%a7%ef%bc%9ajoin%e3%80%81%e5%ad%90%e6%9f%a5%e8%af%a2%e4%b8%8e%e8%81%94%e5%90%88%e6%9f%a5%e8%af%a2%e8%af%a6%e8%a7%a3\/","title":{"rendered":"\u9ad8\u7ea7SQL\u67e5\u8be2\u6280\u5de7\uff1aJOIN\u3001\u5b50\u67e5\u8be2\u4e0e\u8054\u5408\u67e5\u8be2\u8be6\u89e3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u662f<strong>\u9ad8\u7ea7SQL\u67e5\u8be2\u6280\u5de7\uff1aJOIN\u3001\u5b50\u67e5\u8be2\u4e0e\u8054\u5408\u67e5\u8be2\u8be6\u89e3<\/strong>\u90e8\u5206\u7684\u8be6\u7ec6\u6559\u7a0b\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fde\u63a5\u67e5\u8be2\u3001\u5b50\u67e5\u8be2\u3001\u8054\u5408\u67e5\u8be2\u4ee5\u53ca\u7a97\u53e3\u51fd\u6570\u7b49\u9ad8\u7ea7SQL\u6280\u5de7\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u8fde\u63a5\u67e5\u8be2\uff08JOIN\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fde\u63a5\u67e5\u8be2\uff08JOIN\uff09\u662fSQL\u4e2d\u6700\u5e38\u89c1\u7684\u67e5\u8be2\u7c7b\u578b\uff0c\u5b83\u7528\u4e8e\u6839\u636e\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\u4ece\u591a\u4e2a\u8868\u4e2d\u83b7\u53d6\u6570\u636e\u3002SQL\u63d0\u4f9b\u4e86\u4e0d\u540c\u7c7b\u578b\u7684\u8fde\u63a5\u67e5\u8be2\uff0c\u5e38\u89c1\u7684\u5305\u62ec <code>INNER JOIN<\/code>\u3001<code>LEFT JOIN<\/code>\u3001<code>RIGHT JOIN<\/code> \u548c <code>FULL OUTER JOIN<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1.1 <code>INNER JOIN<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>INNER JOIN<\/code> \u8fd4\u56de\u4e24\u4e2a\u8868\u4e2d\u6ee1\u8db3\u8fde\u63a5\u6761\u4ef6\u7684\u4ea4\u96c6\u90e8\u5206\uff0c\u53ea\u6709\u5728\u4e24\u4e2a\u8868\u4e2d\u90fd\u6709\u5339\u914d\u7684\u8bb0\u5f55\u65f6\u624d\u4f1a\u663e\u793a\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u793a\u4f8b\uff1a\u67e5\u8be2\u6240\u6709\u7528\u6237\u53ca\u5176\u5bf9\u5e94\u7684\u8ba2\u5355\u4fe1\u606f\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT users.id, users.name, orders.order_id\nFROM users\nINNER JOIN orders ON users.id = orders.user_id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8be5\u67e5\u8be2\u8fd4\u56de\u6240\u6709\u7528\u6237\u53ca\u5176\u8ba2\u5355\u4fe1\u606f\uff0c\u53ea\u6709\u90a3\u4e9b\u6709\u8ba2\u5355\u7684\u7528\u6237\u624d\u4f1a\u88ab\u8fd4\u56de\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1.2 <code>LEFT JOIN<\/code>\uff08\u6216 <code>LEFT OUTER JOIN<\/code>\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>LEFT JOIN<\/code> \u8fd4\u56de\u5de6\u8868\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u4ee5\u53ca\u53f3\u8868\u4e2d\u4e0e\u5de6\u8868\u8bb0\u5f55\u5339\u914d\u7684\u8bb0\u5f55\u3002\u5982\u679c\u53f3\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u8bb0\u5f55\uff0c\u7ed3\u679c\u4e2d\u5c06\u5305\u542b\u5de6\u8868\u7684\u8bb0\u5f55\uff0c\u5e76\u5c06\u53f3\u8868\u7684\u5217\u8fd4\u56de <code>NULL<\/code>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u793a\u4f8b\uff1a\u67e5\u8be2\u6240\u6709\u7528\u6237\u53ca\u5176\u8ba2\u5355\u4fe1\u606f\uff08\u5305\u62ec\u6ca1\u6709\u8ba2\u5355\u7684\u7528\u6237\uff09\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT users.id, users.name, orders.order_id\nFROM users\nLEFT JOIN orders ON users.id = orders.user_id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u7528\u6237\u6ca1\u6709\u8ba2\u5355\uff0c<code>order_id<\/code> \u5c06\u663e\u793a\u4e3a <code>NULL<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1.3 <code>RIGHT JOIN<\/code>\uff08\u6216 <code>RIGHT OUTER JOIN<\/code>\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>RIGHT JOIN<\/code> \u8fd4\u56de\u53f3\u8868\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u4ee5\u53ca\u5de6\u8868\u4e2d\u4e0e\u53f3\u8868\u8bb0\u5f55\u5339\u914d\u7684\u8bb0\u5f55\u3002\u5982\u679c\u5de6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u8bb0\u5f55\uff0c\u7ed3\u679c\u4e2d\u5c06\u5305\u542b\u53f3\u8868\u7684\u8bb0\u5f55\uff0c\u5e76\u5c06\u5de6\u8868\u7684\u5217\u8fd4\u56de <code>NULL<\/code>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u793a\u4f8b\uff1a\u67e5\u8be2\u6240\u6709\u8ba2\u5355\u53ca\u5176\u5bf9\u5e94\u7684\u7528\u6237\u4fe1\u606f\uff08\u5305\u62ec\u6ca1\u6709\u7528\u6237\u7684\u8ba2\u5355\uff09\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT users.id, users.name, orders.order_id\nFROM users\nRIGHT JOIN orders ON users.id = orders.user_id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8ba2\u5355\u6ca1\u6709\u5bf9\u5e94\u7684\u7528\u6237\uff0c<code>users.id<\/code> \u548c <code>users.name<\/code> \u5c06\u663e\u793a\u4e3a <code>NULL<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1.4 <code>FULL OUTER JOIN<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>FULL OUTER JOIN<\/code> \u8fd4\u56de\u5de6\u8868\u548c\u53f3\u8868\u4e2d\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u5f53\u5176\u4e2d\u4e00\u4e2a\u8868\u6ca1\u6709\u5339\u914d\u65f6\uff0c\u8fd4\u56de <code>NULL<\/code>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u793a\u4f8b\uff1a\u67e5\u8be2\u6240\u6709\u7528\u6237\u548c\u6240\u6709\u8ba2\u5355\u4fe1\u606f\uff08\u5305\u62ec\u6ca1\u6709\u8ba2\u5355\u7684\u7528\u6237\u548c\u6ca1\u6709\u7528\u6237\u7684\u8ba2\u5355\uff09\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT users.id, users.name, orders.order_id\nFROM users\nFULL OUTER JOIN orders ON users.id = orders.user_id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e0\u8bba\u662f\u6ca1\u6709\u8ba2\u5355\u7684\u7528\u6237\uff0c\u8fd8\u662f\u6ca1\u6709\u7528\u6237\u7684\u8ba2\u5355\uff0c\u90fd\u4f1a\u88ab\u8fd4\u56de\uff0c\u5e76\u4e14\u6ca1\u6709\u5339\u914d\u7684\u5217\u4f1a\u663e\u793a\u4e3a <code>NULL<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1.5 \u81ea\u8fde\u63a5\uff08Self Join\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u81ea\u8fde\u63a5\u662f\u6307\u4e00\u4e2a\u8868\u4e0e\u5b83\u81ea\u8eab\u8fde\u63a5\u3002\u901a\u5e38\u5728\u9700\u8981\u6bd4\u8f83\u8868\u4e2d\u7684\u4e0d\u540c\u8bb0\u5f55\u65f6\u4f7f\u7528\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u793a\u4f8b\uff1a\u67e5\u8be2\u5458\u5de5\u4e0e\u5176\u7ecf\u7406\u7684\u4fe1\u606f\uff08\u5047\u8bbe <code>employees<\/code> \u8868\u5305\u542b\u5458\u5de5\u53ca\u5176\u7ecf\u7406\u7684ID\uff09\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT e.name AS employee, m.name AS manager\nFROM employees e\nLEFT JOIN employees m ON e.manager_id = m.id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\uff0c<code>employees<\/code> \u8868\u4e0e\u81ea\u5df1\u8fde\u63a5\uff0c<code>e<\/code> \u8868\u793a\u5458\u5de5\uff0c<code>m<\/code> \u8868\u793a\u7ecf\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u5b50\u67e5\u8be2<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b50\u67e5\u8be2\uff08Subquery\uff09\u662f\u5d4c\u5957\u5728\u5176\u4ed6\u67e5\u8be2\u4e2d\u7684\u67e5\u8be2\uff0c\u5b83\u5141\u8bb8\u4f60\u5728\u4e00\u4e2a\u67e5\u8be2\u4e2d\u4f7f\u7528\u53e6\u4e00\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u3002\u5b50\u67e5\u8be2\u53ef\u4ee5\u653e\u5728 <code>SELECT<\/code>\u3001<code>WHERE<\/code> \u548c <code>FROM<\/code> \u5b50\u53e5\u4e2d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 \u5728 <code>SELECT<\/code> \u5b50\u53e5\u4e2d\u4f7f\u7528\u5b50\u67e5\u8be2<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>SELECT<\/code> \u5b50\u53e5\u4e2d\uff0c\u5b50\u67e5\u8be2\u8fd4\u56de\u4e00\u4e2a\u5355\u4e00\u503c\uff0c\u5e76\u7528\u4e8e\u8ba1\u7b97\u5176\u4ed6\u5217\u7684\u503c\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u793a\u4f8b\uff1a\u67e5\u8be2\u6bcf\u4e2a\u7528\u6237\u7684\u8ba2\u5355\u603b\u6570\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, (SELECT COUNT(*) FROM orders WHERE orders.user_id = users.id) AS order_count\nFROM users;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u67e5\u8be2\u5bf9\u4e8e\u6bcf\u4e2a\u7528\u6237\uff0c\u4f7f\u7528\u5b50\u67e5\u8be2\u8ba1\u7b97\u8be5\u7528\u6237\u7684\u8ba2\u5355\u6570\u91cf\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 \u5728 <code>WHERE<\/code> \u5b50\u53e5\u4e2d\u4f7f\u7528\u5b50\u67e5\u8be2<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b50\u67e5\u8be2\u53ef\u4ee5\u4f5c\u4e3a <code>WHERE<\/code> \u5b50\u53e5\u7684\u6761\u4ef6\uff0c\u901a\u5e38\u7528\u4e8e\u8fc7\u6ee4\u6570\u636e\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u793a\u4f8b\uff1a\u67e5\u8be2\u8d2d\u4e70\u8fc7\u8d85\u8fc7 5 \u4e2a\u8ba2\u5355\u7684\u7528\u6237\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name\nFROM users\nWHERE id IN (SELECT user_id FROM orders GROUP BY user_id HAVING COUNT(*) &gt; 5);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u67e5\u8be2\u901a\u8fc7\u5b50\u67e5\u8be2\u83b7\u53d6\u8d2d\u4e70\u8d85\u8fc7 5 \u4e2a\u8ba2\u5355\u7684\u7528\u6237ID\uff0c\u5e76\u8fd4\u56de\u7b26\u5408\u6761\u4ef6\u7684\u7528\u6237\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.3 \u5728 <code>FROM<\/code> \u5b50\u53e5\u4e2d\u4f7f\u7528\u5b50\u67e5\u8be2<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b50\u67e5\u8be2\u4e5f\u53ef\u4ee5\u653e\u5728 <code>FROM<\/code> \u5b50\u53e5\u4e2d\uff0c\u4f5c\u4e3a\u865a\u62df\u8868\u4f7f\u7528\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u793a\u4f8b\uff1a\u67e5\u8be2\u6bcf\u4e2a\u7528\u6237\u7684\u5e73\u5747\u8ba2\u5355\u91d1\u989d\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT user_id, AVG(order_amount) AS avg_order_amount\nFROM (SELECT user_id, order_amount FROM orders) AS order_data\nGROUP BY user_id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\uff0c\u5b50\u67e5\u8be2\u5c06\u8ba2\u5355\u6570\u636e\u4f5c\u4e3a\u865a\u62df\u8868 <code>order_data<\/code> \u4f7f\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.4 \u76f8\u5173\u5b50\u67e5\u8be2\u4e0e\u975e\u76f8\u5173\u5b50\u67e5\u8be2<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u76f8\u5173\u5b50\u67e5\u8be2<\/strong>\uff1a\u5b50\u67e5\u8be2\u4e2d\u7684\u503c\u4f9d\u8d56\u4e8e\u5916\u90e8\u67e5\u8be2\u7684\u5217\u3002<\/li>\n\n\n\n<li><strong>\u975e\u76f8\u5173\u5b50\u67e5\u8be2<\/strong>\uff1a\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u72ec\u7acb\u4e8e\u5916\u90e8\u67e5\u8be2\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u76f8\u5173\u5b50\u67e5\u8be2\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name\nFROM users u\nWHERE EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.id);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8be5\u67e5\u8be2\u68c0\u67e5\u6bcf\u4e2a\u7528\u6237\u662f\u5426\u81f3\u5c11\u6709\u4e00\u4e2a\u8ba2\u5355\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u8054\u5408\u67e5\u8be2\uff08UNION\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8054\u5408\u67e5\u8be2\u7528\u4e8e\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u5408\u5e76\u4e3a\u4e00\u4e2a\u7ed3\u679c\u96c6\uff0c\u8981\u6c42\u6240\u6709\u67e5\u8be2\u7684\u5217\u6570\u548c\u5217\u7c7b\u578b\u4e00\u81f4\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 \u4f7f\u7528 <code>UNION<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>UNION<\/code> \u7528\u4e8e\u5408\u5e76\u4e24\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\uff0c\u9ed8\u8ba4\u53bb\u9664\u91cd\u590d\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u793a\u4f8b\uff1a\u67e5\u8be2\u6240\u6709\u6765\u81ea\u7f8e\u56fd\u6216\u82f1\u56fd\u7684\u7528\u6237\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name FROM users WHERE country = 'USA'\nUNION\nSELECT name FROM users WHERE country = 'UK';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>UNION<\/code> \u4f1a\u53bb\u6389\u91cd\u590d\u7684\u7528\u6237\uff0c\u8fd4\u56de\u552f\u4e00\u7684\u7528\u6237\u540d\u79f0\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 \u4f7f\u7528 <code>UNION ALL<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>UNION ALL<\/code> \u7528\u4e8e\u5408\u5e76\u4e24\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\uff0c\u4f46\u4e0d\u53bb\u91cd\uff0c\u8fd4\u56de\u6240\u6709\u8bb0\u5f55\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u793a\u4f8b\uff1a\u67e5\u8be2\u6240\u6709\u6765\u81ea\u7f8e\u56fd\u6216\u82f1\u56fd\u7684\u7528\u6237\uff08\u5305\u62ec\u91cd\u590d\u8bb0\u5f55\uff09\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name FROM users WHERE country = 'USA'\nUNION ALL\nSELECT name FROM users WHERE country = 'UK';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>UNION ALL<\/code> \u4f1a\u5305\u62ec\u6240\u6709\u8bb0\u5f55\uff0c\u4e0d\u53bb\u6389\u91cd\u590d\u9879\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u96c6\u5408\u64cd\u4f5c<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u96c6\u5408\u64cd\u4f5c\u7528\u4e8e\u5728 SQL \u4e2d\u6267\u884c\u96c6\u5408\u4e4b\u95f4\u7684\u8fd0\u7b97\uff0c\u7c7b\u4f3c\u4e8e\u6570\u5b66\u4e2d\u7684\u96c6\u5408\u64cd\u4f5c\uff0c\u5305\u62ec\u4ea4\u96c6\uff08<code>INTERSECT<\/code>\uff09\u548c\u5dee\u96c6\uff08<code>EXCEPT<\/code>\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.1 <code>INTERSECT<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>INTERSECT<\/code> \u8fd4\u56de\u4e24\u4e2a\u67e5\u8be2\u7684\u4ea4\u96c6\uff0c\u5373\u4e24\u4e2a\u67e5\u8be2\u90fd\u5305\u542b\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u793a\u4f8b\uff1a\u67e5\u8be2\u8d2d\u4e70\u8fc7\u201c\u5546\u54c1A\u201d\u4e14\u8d2d\u4e70\u8fc7\u201c\u5546\u54c1B\u201d\u7684\u7528\u6237\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT user_id FROM orders WHERE product_id = 'A'\nINTERSECT\nSELECT user_id FROM orders WHERE product_id = 'B';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8be5\u67e5\u8be2\u8fd4\u56de\u540c\u65f6\u8d2d\u4e70\u8fc7\u5546\u54c1A\u548c\u5546\u54c1B\u7684\u7528\u6237\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.2 <code>EXCEPT<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>EXCEPT<\/code> \u8fd4\u56de\u7b2c\u4e00\u4e2a\u67e5\u8be2\u7ed3\u679c\u4e2d\u5b58\u5728\u4f46\u7b2c\u4e8c\u4e2a\u67e5\u8be2\u7ed3\u679c\u4e2d\u4e0d\u5b58\u5728\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u793a\u4f8b\uff1a\u67e5\u8be2\u8d2d\u4e70\u8fc7\u5546\u54c1A\u4f46\u6ca1\u6709\u8d2d\u4e70\u8fc7\u5546\u54c1B\u7684\u7528\u6237\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT user_id FROM orders WHERE product_id = 'A'\nEXCEPT\nSELECT user_id FROM orders WHERE product_id = 'B';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8be5\u67e5\u8be2\u8fd4\u56de\u90a3\u4e9b\u8d2d\u4e70\u8fc7\u5546\u54c1A\u4f46\u6ca1\u6709\u8d2d\u4e70\u8fc7\u5546\u54c1B\u7684\u7528\u6237\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u7a97\u53e3\u51fd\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a97\u53e3\u51fd\u6570\uff08Window Function\uff09\u5141\u8bb8\u4f60\u5728\u67e5\u8be2\u4e2d\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u3001\u5206\u7ec4\u6216\u805a\u5408\uff0c\u800c\u4e0d\u4f1a\u4e22\u5931\u539f\u59cb\u7684\u884c\u4fe1\u606f\u3002\u5e38\u89c1\u7684\u7a97\u53e3\u51fd\u6570\u5305\u62ec <code>ROW_NUMBER()<\/code>\u3001<code>RANK()<\/code> \u548c <code>DENSE_RANK()<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">5.1 <code>ROW_NUMBER()<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ROW_NUMBER()<\/code> \u4e3a\u6bcf\u4e00\u884c\u5206\u914d\u4e00\u4e2a\u552f\u4e00\u7684\u6570\u5b57\uff0c\u901a\u5e38\u6309\u7279\u5b9a\u7684\u987a\u5e8f\u6392\u5217\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u793a\u4f8b\uff1a\u4e3a\u6bcf\u4e2a\u7528\u6237\u7684\u8ba2\u5355\u5206\u914d\u4e00\u4e2a\u884c\u53f7\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT user_id, order_id, ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY order_date) AS row_num\nFROM orders;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u67e5\u8be2\u6839\u636e <code>order_date<\/code> \u5bf9\u6bcf\u4e2a\u7528\u6237\u7684\u8ba2\u5355\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u8ba2\u5355\u5206\u914d\u4e00\u4e2a\u552f\u4e00\u7684\u884c\u53f7\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">5.2 <code>RANK()<\/code> \u548c <code>DENSE_RANK()<\/code><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>RANK()<\/code> \u7ed9\u76f8\u540c\u503c\u7684\u884c\u5206\u914d\u76f8\u540c\u7684\u6392\u540d\uff0c\u5e76\u5728\u6392\u540d\u4e2d\u7559\u4e0b\u7a7a\u7f3a\u3002<\/li>\n\n\n\n<li><code>DENSE_RANK()<\/code> \u7ed9\u76f8\u540c\u503c\u7684\u884c\u5206\u914d\u76f8\u540c\u7684\u6392\u540d\uff0c\u4f46\u4e0d\u4f1a\u7559\u4e0b\u7a7a\u7f3a\u3002<\/li>\n\n\n\n<li>\u793a\u4f8b\uff1a\u6309\u8ba2\u5355\u91d1\u989d\u4e3a\u7528\u6237\u6392\u540d\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT user_id, order_amount, RANK() OVER (ORDER BY order_amount DESC) AS rank\nFROM orders;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>RANK()<\/code> \u4f1a\u5bf9\u8ba2\u5355\u91d1\u989d\u8fdb\u884c\u6392\u540d\uff0c\u4e14\u76f8\u540c\u91d1\u989d\u7684\u8ba2\u5355\u4f1a\u6709\u76f8\u540c\u7684\u6392\u540d\uff0c\u4f46\u6392\u540d\u4e4b\u95f4\u53ef\u80fd\u6709\u7a7a\u7f3a\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">5.3 \u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u8fdb\u884c\u6392\u5e8f\u3001\u5206\u533a\u548c\u805a\u5408<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a97\u53e3\u51fd\u6570\u975e\u5e38\u9002\u5408\u5728\u4e0d\u4e22\u5931\u539f\u59cb\u884c\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u6392\u5e8f\u3001\u5206\u533a\u548c\u805a\u5408\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u793a\u4f8b\uff1a\u8ba1\u7b97\u6bcf\u4e2a\u7528\u6237\u7684\u8ba2\u5355\u91d1\u989d<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6392\u540d\uff0c\u5e76\u5728\u6bcf\u4e2a\u7528\u6237\u5185\u6309\u8ba2\u5355\u91d1\u989d\u6392\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT user_id, order_id, order_amount,\n       ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY order_amount DESC) AS rank\nFROM orders;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u67e5\u8be2\u4f1a\u6839\u636e\u6bcf\u4e2a\u7528\u6237\u7684\u8ba2\u5355\u91d1\u989d\u5bf9\u8ba2\u5355\u8fdb\u884c\u6392\u540d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u4f60\u5b66\u4f1a\u4e86 SQL \u4e2d\u4e00\u4e9b\u91cd\u8981\u7684\u9ad8\u7ea7\u67e5\u8be2\u6280\u5de7\uff0c\u5305\u62ec\u5982\u4f55\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u8fde\u63a5\u67e5\u8be2\u3001\u5982\u4f55\u7f16\u5199\u5b50\u67e5\u8be2\u3001\u5982\u4f55\u4f7f\u7528 <code>UNION<\/code> \u5408\u5e76\u67e5\u8be2\u7ed3\u679c\u3001\u5982\u4f55\u6267\u884c\u96c6\u5408\u64cd\u4f5c\uff08\u5982 <code>INTERSECT<\/code> \u548c <code>EXCEPT<\/code>\uff09\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u7a97\u53e3\u51fd\u6570\u8fdb\u884c\u9ad8\u7ea7\u6392\u5e8f\u548c\u805a\u5408\u64cd\u4f5c\u3002\u638c\u63e1\u8fd9\u4e9b\u9ad8\u7ea7 SQL \u6280\u5de7\uff0c\u5c06\u5927\u5927\u63d0\u5347\u4f60\u5728\u590d\u6742\u67e5\u8be2\u548c\u6570\u636e\u5206\u6790\u4e2d\u7684\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a5\u4e0b\u6765\u662f\u9ad8\u7ea7SQL\u67e5\u8be2\u6280\u5de7\uff1aJOIN\u3001\u5b50\u67e5\u8be2\u4e0e\u8054\u5408\u67e5\u8be2\u8be6\u89e3\u90e8\u5206\u7684\u8be6\u7ec6\u6559\u7a0b\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fde\u63a5\u67e5\u8be2\u3001\u5b50\u67e5\u8be2\u3001\u8054\u5408 [&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-1510","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1510","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=1510"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1510\/revisions"}],"predecessor-version":[{"id":1511,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1510\/revisions\/1511"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}