{"id":60,"date":"2026-05-26T15:17:31","date_gmt":"2026-05-26T07:17:31","guid":{"rendered":"http:\/\/tangjin.xyz\/?p=60"},"modified":"2026-05-26T15:17:31","modified_gmt":"2026-05-26T07:17:31","slug":"sql-right-join","status":"publish","type":"post","link":"http:\/\/tangjin.xyz\/index.php\/2026\/05\/26\/sql-right-join\/","title":{"rendered":"sql right join"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">RIGHT JOIN \u8be6\u89e3<\/h2>\n\n\n\n<p><strong>RIGHT JOIN\uff08\u53f3\u8fde\u63a5\uff09<\/strong> \u662f SQL \u4e2d\u7684\u4e00\u79cd\u8fde\u63a5\u65b9\u5f0f\uff0c\u5b83\u4f1a\u8fd4\u56de<strong>\u53f3\u8868\uff08RIGHT JOIN \u540e\u9762\u7684\u8868\uff09\u7684\u6240\u6709\u8bb0\u5f55<\/strong>\uff0c\u5373\u4f7f\u5de6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u57fa\u672c\u8bed\u6cd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \u5217\u540d\nFROM \u8868A\nRIGHT JOIN \u8868B ON \u8868A.\u5217 = \u8868B.\u5217;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8868A<\/strong>\uff1a\u5de6\u8868<\/li>\n\n\n\n<li><strong>\u8868B<\/strong>\uff1a\u53f3\u8868<\/li>\n\n\n\n<li><strong>ON<\/strong>\uff1a\u6307\u5b9a\u8fde\u63a5\u6761\u4ef6<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u901a\u4fd7\u7406\u89e3<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u8fde\u63a5\u7c7b\u578b<\/th><th>\u8fd4\u56de\u7ed3\u679c<\/th><\/tr><\/thead><tbody><tr><td><strong>INNER JOIN<\/strong><\/td><td>\u4e24\u8868\u90fd\u5339\u914d\u7684\u8bb0\u5f55<\/td><\/tr><tr><td><strong>LEFT JOIN<\/strong><\/td><td>\u5de6\u8868\u5168\u90e8 + \u53f3\u8868\u5339\u914d\u7684<\/td><\/tr><tr><td><strong>RIGHT JOIN<\/strong><\/td><td>\u53f3\u8868\u5168\u90e8 + \u5de6\u8868\u5339\u914d\u7684<\/td><\/tr><tr><td><strong>FULL JOIN<\/strong><\/td><td>\u4e24\u8868\u5168\u90e8\uff08MySQL \u4e0d\u76f4\u63a5\u652f\u6301\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>RIGHT JOIN \u76f8\u5f53\u4e8e LEFT JOIN \u7684\u53cd\u8fc7\u6765<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>A RIGHT JOIN B<\/code> \u7b49\u4ef7\u4e8e <code>B LEFT JOIN A<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u9645\u4f8b\u5b50<\/h2>\n\n\n\n<p>\u5047\u8bbe\u6709\u4e24\u4e2a\u8868\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b66\u751f\u8868 (students)<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>id<\/th><th>name<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>\u5f20\u4e09<\/td><\/tr><tr><td>2<\/td><td>\u674e\u56db<\/td><\/tr><tr><td>3<\/td><td>\u738b\u4e94<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u6210\u7ee9\u8868 (scores)<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>student_id<\/th><th>score<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>90<\/td><\/tr><tr><td>2<\/td><td>85<\/td><\/tr><tr><td>4<\/td><td>88<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">RIGHT JOIN \u793a\u4f8b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT students.name, scores.score\nFROM students\nRIGHT JOIN scores ON students.id = scores.student_id;\n<\/code><\/pre>\n\n\n\n<p><strong>\u7ed3\u679c\uff1a<\/strong><br>| name | score |<br>|&#8212;&#8212;|&#8212;&#8212;-|<br>| \u5f20\u4e09 | 90 |<br>| \u674e\u56db | 85 |<br>| NULL | 88 |<\/p>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53f3\u8868 <code>scores<\/code> \u6709 3 \u6761\u8bb0\u5f55\uff0c\u5168\u90e8\u8fd4\u56de<\/li>\n\n\n\n<li>\u5b66\u751f\u8868\u4e2d id=4 \u6ca1\u6709\u5bf9\u5e94\u5b66\u751f\uff0c\u6240\u4ee5 name \u4e3a NULL<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u53ef\u89c6\u5316\u5bf9\u6bd4<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5de6\u8868 (students)     \u53f3\u8868 (scores)\n+----+------+       +------------+-------+\n| id | name |       | student_id | score |\n+----+------+       +------------+-------+\n| 1  | \u5f20\u4e09 |       | 1          | 90    |\n| 2  | \u674e\u56db |       | 2          | 85    |\n| 3  | \u738b\u4e94 |       | 4          | 88    |\n+----+------+       +------------+-------+\n\nRIGHT JOIN \u7ed3\u679c\uff1a\n+------+-------+\n| name | score |\n+------+-------+\n| \u5f20\u4e09 | 90    |  \u2190 \u5339\u914d\u5230\n| \u674e\u56db | 85    |  \u2190 \u5339\u914d\u5230\n| NULL | 88    |  \u2190 \u53f3\u8868\u6709\uff0c\u5de6\u8868\u65e0\n+------+-------+\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5404\u8fde\u63a5\u5bf9\u6bd4<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-- 1. INNER JOIN\uff1a\u53ea\u8fd4\u56de\u5339\u914d\u7684\nSELECT students.name, scores.score\nFROM students\nINNER JOIN scores ON students.id = scores.student_id;\n-- \u7ed3\u679c\uff1a\u5f20\u4e09(90), \u674e\u56db(85)\n\n-- 2. LEFT JOIN\uff1a\u8fd4\u56de\u5de6\u8868\u5168\u90e8\nSELECT students.name, scores.score\nFROM students\nLEFT JOIN scores ON students.id = scores.student_id;\n-- \u7ed3\u679c\uff1a\u5f20\u4e09(90), \u674e\u56db(85), \u738b\u4e94(NULL)\n\n-- 3. RIGHT JOIN\uff1a\u8fd4\u56de\u53f3\u8868\u5168\u90e8\nSELECT students.name, scores.score\nFROM students\nRIGHT JOIN scores ON students.id = scores.student_id;\n-- \u7ed3\u679c\uff1a\u5f20\u4e09(90), \u674e\u56db(85), NULL(88)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u65f6\u5019\u7528 RIGHT JOIN\uff1f<\/h2>\n\n\n\n<p>\u5b9e\u9645\u4e0a\uff0c<strong>RIGHT JOIN \u4f7f\u7528\u8f83\u5c11<\/strong>\uff0c\u56e0\u4e3a\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u53ef\u4ee5\u7528 LEFT JOIN \u66ff\u4ee3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u8fd9\u4e24\u4e2a\u662f\u7b49\u4ef7\u7684\nA RIGHT JOIN B\n-- \u7b49\u4ef7\u4e8e\nB LEFT JOIN A\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u573a\u666f\u793a\u4f8b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u573a\u666f\uff1a\u627e\u51fa\u6240\u6709\u8ba2\u5355\u53ca\u5176\u5bf9\u5e94\u7684\u5ba2\u6237\uff08\u5305\u62ec\u6ca1\u6709\u5ba2\u6237\u7684\u8ba2\u5355\uff09\nSELECT customers.name, orders.order_date\nFROM customers\nRIGHT JOIN orders ON customers.id = orders.customer_id;\n\n-- \u7b49\u4ef7\u5199\u6cd5\uff08\u66f4\u5e38\u7528\uff09\nSELECT customers.name, orders.order_date\nFROM orders\nLEFT JOIN customers ON customers.id = orders.customer_id;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u56de\u5230\u4f60\u7684\u539f\u67e5\u8be2<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * \nFROM persons AS a \nRIGHT JOIN persons AS b ON b.position = 3;\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u67e5\u8be2\u7684\u610f\u601d\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53f3\u8868\u662f b\uff08\u4e5f\u5c31\u662f persons \u8868\uff09<\/strong><\/li>\n\n\n\n<li>\u8fd4\u56de b \u7684\u6240\u6709\u8bb0\u5f55<\/li>\n\n\n\n<li>\u8fde\u63a5\u6761\u4ef6\u662f <code>b.position = 3<\/code>\uff08\u8fd9\u4e0d\u662f\u4e24\u4e2a\u8868\u4e4b\u95f4\u7684\u8fde\u63a5\u6761\u4ef6\uff0c\u53ea\u662f\u8fc7\u6ee4\uff09<\/li>\n\n\n\n<li>\u56e0\u4e3a\u6ca1\u6709\u771f\u6b63\u7684\u8fde\u63a5\u6761\u4ef6\uff0ca \u8868\u7684\u6bcf\u6761\u8bb0\u5f55\u90fd\u4f1a\u4e0e b \u8868\u7684\u6bcf\u6761\u8bb0\u5f55\u914d\u5bf9\uff08\u7b1b\u5361\u5c14\u79ef\uff09<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5feb\u901f\u8bb0\u5fc6<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5173\u952e\u8bcd<\/th><th>\u8bb0\u5fc6\u65b9\u6cd5<\/th><\/tr><\/thead><tbody><tr><td><strong>LEFT JOIN<\/strong><\/td><td>\u5de6\u8868\u4e3a\u4e3b\uff0c\u5de6\u8868\u5168\u90e8\u4fdd\u7559<\/td><\/tr><tr><td><strong>RIGHT JOIN<\/strong><\/td><td>\u53f3\u8868\u4e3a\u4e3b\uff0c\u53f3\u8868\u5168\u90e8\u4fdd\u7559<\/td><\/tr><tr><td><strong>INNER JOIN<\/strong><\/td><td>\u53ea\u4fdd\u7559\u4e24\u8868\u90fd\u6709\u7684<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>RIGHT JOIN \u8be6\u89e3 RIGHT JOIN\uff08\u53f3\u8fde\u63a5\uff09 \u662f SQL \u4e2d\u7684\u4e00\u79cd\u8fde\u63a5\u65b9\u5f0f\uff0c\u5b83\u4f1a\u8fd4\u56de\u53f3\u8868\uff08RIG [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-60","post","type-post","status-publish","format-standard","hentry","category-note"],"_links":{"self":[{"href":"http:\/\/tangjin.xyz\/index.php\/wp-json\/wp\/v2\/posts\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/tangjin.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/tangjin.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/tangjin.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/tangjin.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=60"}],"version-history":[{"count":1,"href":"http:\/\/tangjin.xyz\/index.php\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":61,"href":"http:\/\/tangjin.xyz\/index.php\/wp-json\/wp\/v2\/posts\/60\/revisions\/61"}],"wp:attachment":[{"href":"http:\/\/tangjin.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/tangjin.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/tangjin.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}