{"id":7492,"date":"2024-11-14T14:00:01","date_gmt":"2024-11-14T06:00:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7492\/"},"modified":"2024-11-14T14:00:01","modified_gmt":"2024-11-14T06:00:01","slug":"mysql-%e5%85%b3%e8%81%94%e6%9f%a5%e8%af%a2%ef%bc%9a%e7%9b%b4%e6%8e%a5%e4%bd%bf%e7%94%a8-join-%e8%bf%98%e6%98%af%e5%88%86%e6%ad%a5%e6%9f%a5%e8%af%a2%e6%9b%b4%e5%90%88%e9%80%82%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7492\/","title":{"rendered":"MySQL \u5173\u8054\u67e5\u8be2\uff1a\u76f4\u63a5\u4f7f\u7528 JOIN \u8fd8\u662f\u5206\u6b65\u67e5\u8be2\u66f4\u5408\u9002\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173089981935248.jpg\" class=\"aligncenter\" title=\"MySQL \u5173\u8054\u67e5\u8be2\uff1a\u76f4\u63a5\u4f7f\u7528 JOIN \u8fd8\u662f\u5206\u6b65\u67e5\u8be2\u66f4\u5408\u9002\uff1f\u63d2\u56fe\" alt=\"MySQL \u5173\u8054\u67e5\u8be2\uff1a\u76f4\u63a5\u4f7f\u7528 JOIN \u8fd8\u662f\u5206\u6b65\u67e5\u8be2\u66f4\u5408\u9002\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong> \u5173\u8054\u67e5\u8be2\uff1a\u76f4\u63a5\u4f7f\u7528 join \u8fd8\u662f\u5206\u6b65\u67e5\u8be2\uff1f<\/strong><\/p>\n<p>\u5f53\u6d89\u53ca\u5230\u5173\u8054\u67e5\u8be2\u65f6\uff0cmysql \u63d0\u4f9b\u4e86\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a\u76f4\u63a5\u4f7f\u7528 join \u6216\u5206\u6b65\u67e5\u8be2\u3002<\/p>\n<p><strong>\u76f4\u63a5\u4f7f\u7528 join<\/strong><\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u66f4\u6709\u6548\uff0c\u56e0\u4e3a\u5b83\u53ea\u9700\u6267\u884c\u4e00\u6b21\u67e5\u8be2\u3002join \u64cd\u4f5c\u4f1a\u81ea\u52a8\u8fde\u63a5\u6ee1\u8db3\u8fde\u63a5\u6761\u4ef6\u7684\u8bb0\u5f55\u3002\u5b83\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u8fd4\u56de\u6765\u81ea\u591a\u4e2a\u8868\u7684\u591a\u4e2a\u5217\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5206\u6b65\u67e5\u8be2<\/strong><\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u6d89\u53ca\u4e24\u6b21\u6216\u66f4\u591a\u6b21\u67e5\u8be2\u3002\u9996\u5148\uff0c\u9009\u62e9\u8981\u8fde\u63a5\u7684\u8868\u7684\u5217\u3002\u7136\u540e\uff0c\u5728\u540e\u7eed\u67e5\u8be2\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u5217\u4f5c\u4e3a where \u5b50\u53e5\u4e2d\u7684\u6761\u4ef6\u3002\u5206\u6b65\u67e5\u8be2\u901a\u5e38\u7528\u4e8e\u9700\u8981\u5bf9\u6570\u636e\u6267\u884c\u66f4\u590d\u6742\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u6548\u7387<\/strong><\/p>\n<p>\u76f4\u63a5\u4f7f\u7528 join \u901a\u5e38\u66f4\u6709\u6548\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u591a\u6b21\u67e5\u8be2\u5f00\u9500\u3002\u7136\u800c\uff0c\u5f53\u9700\u8981\u5bf9\u6570\u636e\u6267\u884c\u590d\u6742\u64cd\u4f5c\u65f6\uff0c\u5206\u6b65\u67e5\u8be2\u53ef\u4ee5\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u6267\u884c\u987a\u5e8f<\/strong><\/p>\n<p>\u5bf9\u4e8e\u4f7f\u7528 join \u7684\u67e5\u8be2\uff0cmysql \u901a\u5e38\u9075\u5faa\u4ee5\u4e0b\u6267\u884c\u987a\u5e8f\uff1a<\/p>\n<ol>\n<li>\u8bc4\u4f30 where \u5b50\u53e5\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\u3002<\/li>\n<li>\u6267\u884c\u8fde\u63a5\u64cd\u4f5c\u3002<\/li>\n<li>\u4ece\u8fde\u63a5\u7ed3\u679c\u4e2d\u9009\u62e9\u6240\u9700\u7684\u5217\u3002<\/li>\n<\/ol>\n<p><strong>\u7a7a\u8868\u6267\u884c\u8ba1\u5212<\/strong><\/p>\n<p>\u5728\u7a7a\u8868\u4e0a\u8fd0\u884c\u6267\u884c\u8ba1\u5212\u663e\u793a\uff1a<\/p>\n<pre>| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |\n| --- | ----------- | ----- | ---------- | ---- | ------------- | --- | ------- | --- | ---- | -------- | ----- |\n| 1 | SIMPLE | auth_user | NULL | ALL | NULL | NULL | NULL | NULL | 0 | 100.00 | Using where |\n| 1 | SIMPLE | friendships_friendship | NULL | ref | PRIMARY,friendships_friendship_to_user_id_986baf39_fk_auth_user_id | friendships_friendship_to_user_id_986baf39_fk_auth_user_id | 5 | const | 0 | 100.00 | Using where; JoinUsingDistinctGroups |<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5\u8ba1\u5212\u663e\u793a\uff1a<\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u5b83\u6267\u884c auth_user \u8868\u4e0a\u7684 where \u5b50\u53e5\uff08to_user_id = 1\uff09\u3002<\/li>\n<li>\u7136\u540e\uff0c\u5b83\u4f7f\u7528 friendships_friendship_to_user_id_fk \u952e\u5728 friendships_friendship \u8868\u4e0a\u6267\u884c\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fMySQL \u5173\u8054\u67e5\u8be2\uff1a\u76f4\u63a5\u4f7f\u7528 JOIN \u8fd8\u662f\u5206\u6b65\u67e5\u8be2\u66f4\u5408\u9002\uff1f\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>\u5173\u8054\u67e5\u8be2\uff1a\u76f4\u63a5\u4f7f\u7528 join \u8fd8\u662f\u5206\u6b65\u67e5\u8be2\uff1f \u5f53\u6d89\u53ca\u5230\u5173\u8054\u67e5\u8be2\u65f6\uff0cmysql \u63d0\u4f9b\u4e86\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a\u76f4\u63a5\u4f7f\u7528 join \u6216\u5206\u6b65\u67e5\u8be2\u3002 \u76f4\u63a5\u4f7f\u7528 join \u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u66f4\u6709\u6548\uff0c\u56e0\u4e3a\u5b83\u53ea\u9700\u6267\u884c\u4e00\u6b21\u67e5\u8be2\u3002join \u64cd\u4f5c\u4f1a\u81ea\u52a8\u8fde\u63a5\u6ee1\u8db3\u8fde\u63a5\u6761\u4ef6\u7684\u8bb0\u5f55\u3002\u5b83\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u8fd4\u56de\u6765\u81ea\u591a\u4e2a\u8868\u7684\u591a\u4e2a\u5217\u7684\u60c5\u51b5\u3002 \u5206\u6b65\u67e5\u8be2 \u8fd9\u79cd\u65b9\u6cd5\u6d89\u53ca\u4e24\u6b21\u6216\u66f4\u591a\u6b21\u67e5\u8be2\u3002\u9996\u5148\uff0c\u9009\u62e9\u8981\u8fde\u63a5\u7684\u8868\u7684\u5217\u3002\u7136\u540e\uff0c\u5728\u540e\u7eed\u67e5\u8be2\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u5217\u4f5c\u4e3a where \u5b50\u53e5\u4e2d\u7684\u6761\u4ef6\u3002\u5206\u6b65\u67e5\u8be2\u901a\u5e38\u7528\u4e8e\u9700\u8981\u5bf9\u6570\u636e\u6267\u884c\u66f4\u590d\u6742\u64cd\u4f5c\u7684\u60c5\u51b5\u3002 \u6548\u7387 \u76f4\u63a5\u4f7f\u7528 join \u901a\u5e38\u66f4\u6709\u6548\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u591a\u6b21\u67e5\u8be2\u5f00\u9500\u3002\u7136\u800c\uff0c\u5f53\u9700\u8981\u5bf9\u6570\u636e\u6267\u884c\u590d\u6742\u64cd\u4f5c\u65f6\uff0c\u5206\u6b65\u67e5\u8be2\u53ef\u4ee5\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002 \u6267\u884c\u987a\u5e8f \u5bf9\u4e8e\u4f7f\u7528 join \u7684\u67e5\u8be2\uff0cmysql \u901a\u5e38\u9075\u5faa\u4ee5\u4e0b\u6267\u884c\u987a\u5e8f\uff1a \u8bc4\u4f30 where \u5b50\u53e5\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\u3002 \u6267\u884c\u8fde\u63a5\u64cd\u4f5c\u3002 \u4ece\u8fde\u63a5\u7ed3\u679c\u4e2d\u9009\u62e9\u6240\u9700\u7684\u5217\u3002 \u7a7a\u8868\u6267\u884c\u8ba1\u5212 \u5728\u7a7a\u8868\u4e0a\u8fd0\u884c\u6267\u884c\u8ba1\u5212\u663e\u793a\uff1a | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra [&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-7492","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7492","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=7492"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7492\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}