{"id":6259,"date":"2024-11-14T12:40:42","date_gmt":"2024-11-14T04:40:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6259\/"},"modified":"2024-11-14T12:40:42","modified_gmt":"2024-11-14T04:40:42","slug":"sql-join%e6%80%8e%e6%a0%b7%e5%a4%84%e7%90%86%e7%a9%ba%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6259\/","title":{"rendered":"sql join\u600e\u6837\u5904\u7406\u7a7a\u503c"},"content":{"rendered":"<blockquote><p>\n  sql join \u4e2d\u7684\u7a7a\u503c\u5904\u7406\u53d6\u51b3\u4e8e join \u7c7b\u578b\uff1ainner join\uff1a\u4ec5\u5339\u914d\u975e\u7a7a\u503c\u8bb0\u5f55\u3002left join\uff1a\u5de6\u8868\u586b\u5145\u53f3\u8868\u7a7a\u503c\u3002right join\uff1a\u53f3\u8868\u586b\u5145\u5de6\u8868\u7a7a\u503c\u3002full join\uff1a\u8fd4\u56de\u6240\u6709\u884c\uff0c\u7a7a\u503c\u586b\u5145\u4e3a null\u3002\u53ef\u4ee5\u901a\u8fc7 coalesce \u51fd\u6570\u3001isnull \u51fd\u6570\u6216 case \u8868\u8fbe\u5f0f\u7b49\u65b9\u6cd5\u5904\u7406\u7a7a\u503c\uff0c\u4ee5\u8fd4\u56de\u4e00\u81f4\u7684\u67e5\u8be2\u7ed3\u679c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/17\/2024061710393086350.jpg\" class=\"aligncenter\" title=\"sql join\u600e\u6837\u5904\u7406\u7a7a\u503c\u63d2\u56fe\" alt=\"sql join\u600e\u6837\u5904\u7406\u7a7a\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL JOIN \u64cd\u4f5c\u4e2d\u7684\u7a7a\u503c\u5904\u7406<\/strong><\/p>\n<p>\u5728\u6267\u884c SQL JOIN \u64cd\u4f5c\u65f6\uff0c\u7a7a\u503c\u7684\u5b58\u5728\u4f1a\u5f71\u54cd\u67e5\u8be2\u7ed3\u679c\u3002\u7a7a\u503c\u662f\u6307\u6570\u636e\u5e93\u4e2d\u672a\u5b58\u50a8\u4efb\u4f55\u6570\u636e\u7684\u5b57\u6bb5\u6216\u5217\u3002\u5904\u7406\u7a7a\u503c\u7684\u65b9\u5f0f\u53d6\u51b3\u4e8e JOIN \u7c7b\u578b\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>INNER JOIN<\/strong><\/p>\n<p>INNER JOIN \u4ec5\u8fd4\u56de\u540c\u65f6\u5728\u4e24\u4e2a\u8868\u4e2d\u90fd\u6709\u5339\u914d\u884c\u7684\u8bb0\u5f55\u3002\u5982\u679c\u4efb\u4f55\u8868\u4e2d\u7684\u884c\u5305\u542b\u7a7a\u503c\uff0c\u8be5\u884c\u5c06\u4e0d\u4f1a\u5305\u542b\u5728\u7ed3\u679c\u96c6\u4e2d\u3002<\/p>\n<p><strong>LEFT JOIN<\/strong><\/p>\n<p>LEFT JOIN \u8fd4\u56de\u5de6\u8868\u4e2d\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u53f3\u8868\u4e2d\u4e0d\u5b58\u5728\u5339\u914d\u884c\u3002\u7a7a\u503c\u5c06\u4ee5 NULL \u503c\u586b\u5145\u53f3\u8868\u4e2d\u7684\u5217\u3002<\/p>\n<p><strong>RIGHT JOIN<\/strong><\/p>\n<p>RIGHT JOIN \u8fd4\u56de\u53f3\u8868\u4e2d\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5de6\u8868\u4e2d\u4e0d\u5b58\u5728\u5339\u914d\u884c\u3002\u7a7a\u503c\u5c06\u4ee5 NULL \u503c\u586b\u5145\u5de6\u8868\u4e2d\u7684\u5217\u3002<\/p>\n<p><strong>FULL JOIN<\/strong><\/p>\n<p>FULL JOIN \u8fd4\u56de\u4e24\u4e2a\u8868\u4e2d\u6240\u6709\u884c\uff0c regardless of whether they have matching rows in the other table. \u7a7a\u503c\u5c06\u4ee5 NULL \u503c\u586b\u5145\u7ed3\u679c\u96c6\u4e2d\u7f3a\u5931\u7684\u5217\u3002<\/p>\n<p><strong>\u5904\u7406\u7a7a\u503c\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u4e3a\u4e86\u5904\u7406\u7a7a\u503c\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li> <strong>\u4f7f\u7528 COALESCE \u51fd\u6570\uff1a<\/strong> COALESCE \u51fd\u6570\u8fd4\u56de\u7b2c\u4e00\u4e2a\u975e\u7a7a\u503c\u7684\u8868\u8fbe\u5f0f\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u586b\u5145\u7a7a\u503c\u3002<\/li>\n<li> <strong>\u4f7f\u7528 ISNULL \u51fd\u6570\uff1a<\/strong> ISNULL \u51fd\u6570\u68c0\u67e5\u5b57\u6bb5\u662f\u5426\u4e3a NULL\u3002\u5982\u679c\u4e3a NULL\uff0c\u5219\u8fd4\u56de\u6307\u5b9a\u7684\u503c\u3002<\/li>\n<li> <strong>\u4f7f\u7528 CASE \u8868\u8fbe\u5f0f\uff1a<\/strong> CASE \u8868\u8fbe\u5f0f\u5141\u8bb8\u6839\u636e\u5b57\u6bb5\u7684\u503c\u5411\u4e0d\u540c\u7684\u7ed3\u679c\u6c42\u503c\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u5904\u7406\u7a7a\u503c\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528 COALESCE \u51fd\u6570\u5904\u7406\u7a7a\u503c\uff1a<\/p>\n<pre>SELECT COALESCE(customer_name, 'Unknown') AS customer_name\nFROM customers;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u67e5\u8be2\u8fd4\u56de\u5ba2\u6237\u59d3\u540d\u5217\uff0c\u5982\u679c\u5ba2\u6237\u59d3\u540d\u4e3a\u7a7a\uff0c\u5219\u7528 &#8220;Unknown&#8221; \u586b\u5145\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u6b63\u786e\u5904\u7406 SQL JOIN \u64cd\u4f5c\u4e2d\u7684\u7a7a\u503c\u5bf9\u4e8e\u786e\u4fdd\u67e5\u8be2\u7ed3\u679c\u7684\u51c6\u786e\u6027\u81f3\u5173\u91cd\u8981\u3002\u901a\u8fc7\u4f7f\u7528\u9002\u5f53\u7684 JOIN \u7c7b\u578b\u548c\u7a7a\u503c\u5904\u7406\u6280\u672f\uff0c\u53ef\u4ee5\u907f\u514d\u7a7a\u503c\u5bfc\u81f4\u7684\u95ee\u9898\u5e76\u786e\u4fdd\u8fd4\u56de\u4e00\u81f4\u4e14\u6709\u610f\u4e49\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql join\u600e\u6837\u5904\u7406\u7a7a\u503c\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 join \u4e2d\u7684\u7a7a\u503c\u5904\u7406\u53d6\u51b3\u4e8e join \u7c7b\u578b\uff1ainner join\uff1a\u4ec5\u5339\u914d\u975e\u7a7a\u503c\u8bb0\u5f55\u3002left join\uff1a\u5de6\u8868\u586b\u5145\u53f3\u8868\u7a7a\u503c\u3002right join\uff1a\u53f3\u8868\u586b\u5145\u5de6\u8868\u7a7a\u503c\u3002full join\uff1a\u8fd4\u56de\u6240\u6709\u884c\uff0c\u7a7a\u503c\u586b\u5145\u4e3a null\u3002\u53ef\u4ee5\u901a\u8fc7 coalesce \u51fd\u6570\u3001isnull \u51fd\u6570\u6216 case \u8868\u8fbe\u5f0f\u7b49\u65b9\u6cd5\u5904\u7406\u7a7a\u503c\uff0c\u4ee5\u8fd4\u56de\u4e00\u81f4\u7684\u67e5\u8be2\u7ed3\u679c\u3002 SQL JOIN \u64cd\u4f5c\u4e2d\u7684\u7a7a\u503c\u5904\u7406 \u5728\u6267\u884c SQL JOIN \u64cd\u4f5c\u65f6\uff0c\u7a7a\u503c\u7684\u5b58\u5728\u4f1a\u5f71\u54cd\u67e5\u8be2\u7ed3\u679c\u3002\u7a7a\u503c\u662f\u6307\u6570\u636e\u5e93\u4e2d\u672a\u5b58\u50a8\u4efb\u4f55\u6570\u636e\u7684\u5b57\u6bb5\u6216\u5217\u3002\u5904\u7406\u7a7a\u503c\u7684\u65b9\u5f0f\u53d6\u51b3\u4e8e JOIN \u7c7b\u578b\u7684\u9009\u62e9\u3002 INNER JOIN INNER JOIN \u4ec5\u8fd4\u56de\u540c\u65f6\u5728\u4e24\u4e2a\u8868\u4e2d\u90fd\u6709\u5339\u914d\u884c\u7684\u8bb0\u5f55\u3002\u5982\u679c\u4efb\u4f55\u8868\u4e2d\u7684\u884c\u5305\u542b\u7a7a\u503c\uff0c\u8be5\u884c\u5c06\u4e0d\u4f1a\u5305\u542b\u5728\u7ed3\u679c\u96c6\u4e2d\u3002 LEFT JOIN LEFT JOIN \u8fd4\u56de\u5de6\u8868\u4e2d\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u53f3\u8868\u4e2d\u4e0d\u5b58\u5728\u5339\u914d\u884c\u3002\u7a7a\u503c\u5c06\u4ee5 NULL \u503c\u586b\u5145\u53f3\u8868\u4e2d\u7684\u5217\u3002 RIGHT JOIN RIGHT JOIN \u8fd4\u56de\u53f3\u8868\u4e2d\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5de6\u8868\u4e2d\u4e0d\u5b58\u5728\u5339\u914d\u884c\u3002\u7a7a\u503c\u5c06\u4ee5 NULL \u503c\u586b\u5145\u5de6\u8868\u4e2d\u7684\u5217\u3002 FULL JOIN FULL JOIN \u8fd4\u56de\u4e24\u4e2a\u8868\u4e2d\u6240\u6709\u884c\uff0c regardless of whether they have matching rows [&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-6259","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6259","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=6259"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6259\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}