{"id":16,"date":"2026-11-08T13:15:42","date_gmt":"2026-11-08T13:15:42","guid":{"rendered":"http:\/\/blog.antpython.net\/?p=16"},"modified":"2026-11-08T13:15:42","modified_gmt":"2026-11-08T13:15:42","slug":"sql-%e5%a4%9a%e8%a1%a8-join-%e4%b8%8e%e5%ad%90%e6%9f%a5%e8%af%a2%e6%80%8e%e4%b9%88%e9%80%89","status":"publish","type":"post","link":"http:\/\/blog.antpython.net\/?p=16","title":{"rendered":"SQL \u591a\u8868 JOIN \u4e0e\u5b50\u67e5\u8be2\u600e\u4e48\u9009"},"content":{"rendered":"<p>\u771f\u5b9e\u4e1a\u52a1\u6570\u636e\u5206\u6563\u5728\u591a\u5f20\u8868\uff1a\u7528\u6237\u3001\u8ba2\u5355\u3001\u5546\u54c1\u3001\u652f\u4ed8\u3002\u4f1a\u5199 JOIN\uff0c\u624d\u80fd\u628a\u300c\u6545\u4e8b\u300d\u62fc\u5b8c\u6574\u3002<\/p>\n<h2>\u56db\u79cd JOIN \u76f4\u89c9<\/h2>\n<ul>\n<li><strong>INNER JOIN<\/strong>\uff1a\u4e24\u8fb9\u90fd\u5339\u914d\u624d\u4fdd\u7559<\/li>\n<li><strong>LEFT JOIN<\/strong>\uff1a\u4fdd\u7559\u5de6\u8868\u5168\u90e8\uff0c\u53f3\u8868\u6ca1\u6709\u5219 NULL<\/li>\n<li><strong>RIGHT JOIN<\/strong>\uff1a\u5c11\u7528\uff0c\u80fd\u5199\u6210 LEFT \u5c31\u5199\u6210 LEFT<\/li>\n<li><strong>FULL JOIN<\/strong>\uff1aMySQL \u4e0d\u76f4\u63a5\u652f\u6301\uff0c\u5e38\u7528 UNION \u6a21\u62df<\/li>\n<\/ul>\n<h2>\u793a\u4f8b\uff1a\u7528\u6237\u4e0e\u8ba2\u5355<\/h2>\n<pre><code>SELECT u.id, u.name, o.order_id, o.amount\nFROM users u\nLEFT JOIN orders o ON u.id = o.user_id\nWHERE u.created_at &gt;= '2026-01-01';\n<\/code><\/pre>\n<p>\u60f3\u627e\u300c\u6ce8\u518c\u4e86\u4f46\u6ca1\u4e0b\u5355\u300d\u7684\u7528\u6237\uff1a<\/p>\n<pre><code>SELECT u.*\nFROM users u\nLEFT JOIN orders o ON u.id = o.user_id\nWHERE o.order_id IS NULL;\n<\/code><\/pre>\n<h2>\u5b50\u67e5\u8be2 vs JOIN<\/h2>\n<pre><code>-- \u5b50\u67e5\u8be2\uff1a\u5148\u627e\u9ad8\u4ef7\u503c\u7528\u6237\uff0c\u518d\u67e5\u660e\u7ec6\nSELECT *\nFROM orders\nWHERE user_id IN (\n  SELECT user_id FROM orders GROUP BY user_id HAVING SUM(amount) &gt; 10000\n);\n\n-- \u7b49\u4ef7\u601d\u8def\u7528 JOIN + \u805a\u5408\u6d3e\u751f\u8868\uff0c\u5f80\u5f80\u66f4\u597d\u4f18\u5316\nSELECT o.*\nFROM orders o\nJOIN (\n  SELECT user_id\n  FROM orders\n  GROUP BY user_id\n  HAVING SUM(amount) &gt; 10000\n) vip ON o.user_id = vip.user_id;\n<\/code><\/pre>\n<h2>\u9009\u578b\u5efa\u8bae<\/h2>\n<ol>\n<li>\u5173\u8054\u53d6\u5b57\u6bb5\uff1a\u4f18\u5148 JOIN<\/li>\n<li>\u5b58\u5728\u6027\u5224\u65ad\uff1a\u53ef\u7528 <code>EXISTS<\/code><\/li>\n<li>\u5b50\u67e5\u8be2\u7ed3\u679c\u5f88\u5927\u65f6\uff0c\u6ce8\u610f\u91cd\u590d\u8ba1\u7b97\uff0c\u8003\u8651\u4e34\u65f6\u8868\/\u6d3e\u751f\u8868<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u771f\u5b9e\u4e1a\u52a1\u6570\u636e\u5206\u6563\u5728\u591a\u5f20\u8868\uff1a\u7528\u6237\u3001\u8ba2\u5355\u3001\u5546\u54c1\u3001\u652f\u4ed8\u3002\u4f1a\u5199 JOIN\uff0c\u624d\u80fd\u628a\u300c\u6545\u4e8b\u300d\u62fc\u5b8c\u6574\u3002 \u56db\u79cd JOIN \u76f4\u89c9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16"}],"version-history":[{"count":0,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}