{"id":201051,"date":"2025-05-06T10:01:09","date_gmt":"2025-05-06T02:01:09","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201051\/"},"modified":"2025-05-06T10:01:09","modified_gmt":"2025-05-06T02:01:09","slug":"mysql-join-%e6%9f%a5%e8%af%a2%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%ef%bc%9a%e8%8e%b7%e5%8f%96%e7%94%a8%e6%88%b7%e7%b2%89%e4%b8%9d%e4%bf%a1%e6%81%af%ef%bc%8c%e4%bd%bf%e7%94%a8-join-%e8%bf%98%e6%98%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201051\/","title":{"rendered":"MySQL JOIN \u67e5\u8be2\u6027\u80fd\u4f18\u5316\uff1a\u83b7\u53d6\u7528\u6237\u7c89\u4e1d\u4fe1\u606f\uff0c\u4f7f\u7528 JOIN \u8fd8\u662f\u62c6\u5206\u67e5\u8be2\u66f4\u4f18\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>MySQL JOIN \u67e5\u8be2\u6027\u80fd\u4f18\u5316\uff1a\u83b7\u53d6\u7528\u6237\u7c89\u4e1d\u4fe1\u606f\uff0c\u4f7f\u7528 JOIN \u8fd8\u662f\u62c6\u5206\u67e5\u8be2\u66f4\u4f18\uff1f<\/h1>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/173271749167472bb35f993.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>mysql join \u67e5\u8be2\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528 join \u8fd8\u662f\u62c6\u5206\u67e5\u8be2\uff1f<\/strong><\/p>\n<p>\u5bf9\u4e8e\u83b7\u53d6\u7279\u5b9a\u7528\u6237\u7684\u7c89\u4e1d\u4fe1\u606f\u7684\u67e5\u8be2\uff0c\u53ef\u4ee5\u4f7f\u7528 join \u64cd\u4f5c\u6216\u62c6\u5206\u67e5\u8be2\u3002\u4ee5\u4e0b\u5206\u6790\u5bf9\u6bd4\u4e86\u4e24\u79cd\u65b9\u6cd5\u7684\u6027\u80fd\uff1a<\/p>\n<p><strong>join \u67e5\u8be2 (\u65b9\u5f0f\u4e00)<\/strong><\/p>\n<pre>select\n    `friendships_friendship`.`id`,\n    `friendships_friendship`.`from_user_id`,\n    `friendships_friendship`.`to_user_id`,\n    `friendships_friendship`.`created_at`,\n    t3.`id`,\n    t3.`password`,\n    t3.`last_login`,\n    t3.`is_superuser`,\n    t3.`username`,\n    t3.`first_name`,\n    t3.`last_name`,\n    t3.`email`,\n    t3.`is_staff`,\n    t3.`is_active`,\n    t3.`date_joined`\nfrom\n    `friendships_friendship`\n    left outer join `auth_user` t3 on (\n        `friendships_friendship`.`from_user_id` = t3.`id`\n    )\nwhere\n    `friendships_friendship`.`to_user_id` = 1\nlimit\n    21;<\/pre>\n<p>join \u67e5\u8be2\u4ec5\u6267\u884c\u4e86\u4e00\u6b21\u67e5\u8be2\uff0c\u867d\u7136\u4f7f\u7528\u4e86\u8fde\u63a5\u64cd\u4f5c\uff0c\u4f46\u53ea\u8fde\u63a5\u4e86\u6ee1\u8db3\u6761\u4ef6\u7684\u8bb0\u5f55\u3002\u56e0\u6b64\uff0c\u6574\u4f53\u6548\u7387\u4e0d\u4f1a\u6bd4\u62c6\u5206\u67e5\u8be2\u5dee\u591a\u5c11\u3002<\/p>\n<p><strong>\u62c6\u5206\u67e5\u8be2 (\u65b9\u5f0f\u4e8c)<\/strong><\/p>\n<p>\u6b64\u65b9\u6cd5\u5206\u4e3a\u4e24\u6b65\uff1a<\/p>\n<p><strong>\u6b65\u9aa4 1\uff1a<\/strong> \u83b7\u53d6\u597d\u53cb\u5173\u7cfb\u8868\u4e2d\u6ee1\u8db3\u6761\u4ef6\u7684\u8bb0\u5f55\u3002<\/p>\n<pre>select\n    `friendships_friendship`.`id`,\n    `friendships_friendship`.`from_user_id`,\n    `friendships_friendship`.`to_user_id`,\n    `friendships_friendship`.`created_at`\nfrom\n    `friendships_friendship`\nwhere\n    `friendships_friendship`.`to_user_id` = 1\nlimit\n    21;<\/pre>\n<p><strong>\u6b65\u9aa4 2\uff1a<\/strong> \u4f7f\u7528\u6b65\u9aa4 1 \u83b7\u5f97\u7684 from_user_id\uff0c\u5728\u7528\u6237\u8868\u4e2d\u67e5\u8be2\u7528\u6237\u4fe1\u606f\u3002<\/p>\n<pre>SELECT\n    T3.`id`,\n    T3.`password`,\n    T3.`last_login`,\n    T3.`is_superuser`,\n    T3.`username`,\n    T3.`first_name`,\n    T3.`last_name`,\n    T3.`email`,\n    T3.`is_staff`,\n    T3.`is_active`,\n    T3.`date_joined`\nFROM\n    `auth_user` T3\nWHERE\n    T3.`from_user_id` in (xxxx, xxx, xxxx)\nLIMIT\n    21;<\/pre>\n<p>\u62c6\u5206\u67e5\u8be2\u5206\u4e24\u6b65\u8fdb\u884c\uff0c\u9700\u8981\u5206\u522b\u6267\u884c\u4e24\u6b21\u67e5\u8be2\uff0c\u6548\u7387\u7a0d\u4f4e\u3002<\/p>\n<p>\u603b\u7684\u6765\u8bf4\uff0c\u5bf9\u4e8e\u8fd9\u79cd\u7c7b\u578b\u7684\u67e5\u8be2\uff0c\u4f7f\u7528 join \u67e5\u8be2\u7684\u6548\u7387\u4f1a\u7565\u9ad8\u4e8e\u62c6\u5206\u67e5\u8be2\u3002<\/p>\n<p><strong>mysql \u6267\u884c\u987a\u5e8f<\/strong><\/p>\n<p>mysql \u7684\u6267\u884c\u987a\u5e8f\u662f\u5148\u6267\u884c where \u5b50\u53e5\uff0c\u7136\u540e\u518d\u6267\u884c join \u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u5bf9\u4e8e\u65b9\u5f0f\u4e00\u7684\u67e5\u8be2\uff0cmysql \u4f1a\u5148\u627e\u5230 friendships_friendship \u8868\u4e2d to_user_id=1 \u7684\u8bb0\u5f55\uff0c\u518d\u4e0e auth_user \u8868\u8fdb\u884c join \u64cd\u4f5c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u4e3b\u673a\u5b9d\u8d1d\uff01\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>MySQL JOIN \u67e5\u8be2\u6027\u80fd\u4f18&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201051","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201051"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201051\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}