{"id":26338,"date":"2024-11-24T09:48:38","date_gmt":"2024-11-24T01:48:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/26338\/"},"modified":"2024-11-24T09:48:38","modified_gmt":"2024-11-24T01:48:38","slug":"%e4%bd%bf%e7%94%a8-join-%e8%bf%98%e6%98%af%e5%a4%9a%e6%ac%a1%e6%9f%a5%e8%a1%a8%ef%bc%9f%e5%85%b3%e8%81%94%e6%9f%a5%e8%af%a2%e6%95%88%e7%8e%87%e5%93%aa%e7%a7%8d%e6%9b%b4%e9%ab%98%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/26338\/","title":{"rendered":"\u4f7f\u7528 JOIN \u8fd8\u662f\u591a\u6b21\u67e5\u8868\uff1f\u5173\u8054\u67e5\u8be2\u6548\u7387\u54ea\u79cd\u66f4\u9ad8\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173088903033022.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 JOIN \u8fd8\u662f\u591a\u6b21\u67e5\u8868\uff1f\u5173\u8054\u67e5\u8be2\u6548\u7387\u54ea\u79cd\u66f4\u9ad8\uff1f\u63d2\u56fe\" alt=\"\u4f7f\u7528 JOIN \u8fd8\u662f\u591a\u6b21\u67e5\u8868\uff1f\u5173\u8054\u67e5\u8be2\u6548\u7387\u54ea\u79cd\u66f4\u9ad8\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5173\u8054\u67e5\u8be2\uff1a\u4f7f\u7528 join \u8fd8\u662f\u591a\u6b21\u67e5\u8868\uff1f<\/strong><\/p>\n<p><strong>\u9700\u6c42\uff1a<\/strong>\u83b7\u53d6\u67d0\u4e2a\u4eba\u7684\u7c89\u4e1d\u4fe1\u606f<\/p>\n<p><strong>\u8868\u7ed3\u6784\uff1a<\/strong><\/p>\n<pre>create table `auth_user` (...);\ncreate table `friendships_friendship` (...);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e24\u79cd\u65b9\u5f0f\uff1a<\/strong><\/p>\n<p><strong>\u65b9\u5f0f\u4e00\uff1a\u4f7f\u7528 join<\/strong><\/p>\n<pre>select ...\nfrom `friendships_friendship`\nleft join `auth_user` t3 on (\n    `friendships_friendship`.`from_user_id` = t3.`id`\n)\nwhere\n    `friendships_friendship`.`to_user_id` = 1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u5f0f\u4e8c\uff1a\u62c6\u5206\u6210\u4e24\u6b21\u67e5\u8868<\/strong><\/p>\n<p><strong>\u7b2c\u4e00\u6b21\u83b7\u53d6 id<\/strong><\/p>\n<pre>select ...\nfrom `friendships_friendship`\nwhere\n    `friendships_friendship`.`to_user_id` = 1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7b2c\u4e8c\u6b21\u4f7f\u7528 id \u67e5\u8be2<\/strong><\/p>\n<pre>SELECT ...\nFROM `auth_user`\nWHERE\n    `auth_user`.`id` IN (...);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6548\u7387\u5bf9\u6bd4\uff1a<\/strong><\/p>\n<p>\u7b2c\u4e00\u79cd\u65b9\u5f0f\u6548\u7387\u66f4\u9ad8\uff0c\u56e0\u4e3a\u5b83\u53ea\u6267\u884c\u4e86\u4e00\u6b21\u67e5\u8be2\uff0c\u800c\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u9700\u8981\u6267\u884c\u4e24\u6b21\u67e5\u8be2\u3002\u867d\u7136\u7b2c\u4e00\u79cd\u65b9\u5f0f\u4f7f\u7528\u4e86 join \u64cd\u4f5c\uff0c\u4f46\u5728  \u4e2d\uff0cjoin \u4ec5\u5bf9\u6ee1\u8db3\u67e5\u8be2\u6761\u4ef6\u7684\u8bb0\u5f55\u6267\u884c\u3002\u56e0\u6b64\uff0c\u4e0e\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u4f7f\u7528 in \u64cd\u4f5c\u7b26\u76f8\u6bd4\uff0c\u4e0d\u4f1a\u6709\u592a\u5927\u7684\u6027\u80fd\u5dee\u5f02\u3002<\/p>\n<p><strong>\u67e5\u8be2\u987a\u5e8f\uff1a<\/strong><\/p>\n<p>\u5bf9\u4e8e\u65b9\u5f0f\u4e00\uff0cmysql \u7684\u67e5\u8be2\u987a\u5e8f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u627e\u5230\u6ee1\u8db3 friendships_friendship.to_user_id = 1 \u6761\u4ef6\u7684\u8bb0\u5f55\u3002<\/li>\n<li>\u5bf9\u627e\u5230\u7684\u8bb0\u5f55\u6267\u884c join \u64cd\u4f5c\uff0c\u4e0e auth_user \u8868\u8fdb\u884c\u5173\u8054\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 JOIN \u8fd8\u662f\u591a\u6b21\u67e5\u8868\uff1f\u5173\u8054\u67e5\u8be2\u6548\u7387\u54ea\u79cd\u66f4\u9ad8\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u8054\u67e5\u8be2\uff1a\u4f7f\u7528 join \u8fd8\u662f\u591a\u6b21\u67e5\u8868\uff1f \u9700\u6c42\uff1a\u83b7\u53d6\u67d0\u4e2a\u4eba\u7684\u7c89\u4e1d\u4fe1\u606f \u8868\u7ed3\u6784\uff1a create table `auth_user` (&#8230;); create table `friendships_friendship` (&#8230;); \u767b\u5f55\u540e\u590d\u5236 \u4e24\u79cd\u65b9\u5f0f\uff1a \u65b9\u5f0f\u4e00\uff1a\u4f7f\u7528 join select &#8230; from `friendships_friendship` left join `auth_user` t3 on ( `friendships_friendship`.`from_user_id` = t3.`id` ) where `friendships_friendship`.`to_user_id` = 1; \u767b\u5f55\u540e\u590d\u5236 \u65b9\u5f0f\u4e8c\uff1a\u62c6\u5206\u6210\u4e24\u6b21\u67e5\u8868 \u7b2c\u4e00\u6b21\u83b7\u53d6 id select &#8230; from `friendships_friendship` where `friendships_friendship`.`to_user_id` = 1; \u767b\u5f55\u540e\u590d\u5236 \u7b2c\u4e8c\u6b21\u4f7f\u7528 id \u67e5\u8be2 SELECT &#8230; FROM `auth_user` [&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-26338","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26338","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=26338"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26338\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=26338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=26338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=26338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}