{"id":25496,"date":"2024-11-24T09:47:52","date_gmt":"2024-11-24T01:47:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25496\/"},"modified":"2024-11-24T09:47:52","modified_gmt":"2024-11-24T01:47:52","slug":"mysql-%e5%85%b3%e8%81%94%e8%a1%a8%e6%9f%a5%e8%af%a2%e7%96%91%e9%9a%be%e8%a7%a3%e6%9e%90%ef%bc%9a%e5%a6%82%e4%bd%95%e4%bb%8e-a-%e8%a1%a8%e5%92%8c-b-%e8%a1%a8%e4%b8%ad%e8%8e%b7%e5%8f%96%e7%89%b9-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25496\/","title":{"rendered":"MySQL \u5173\u8054\u8868\u67e5\u8be2\u7591\u96be\u89e3\u6790\uff1a\u5982\u4f55\u4ece A \u8868\u548c B \u8868\u4e2d\u83b7\u53d6\u7279\u5b9a\u6761\u4ef6\u7684\u8bb0\u5f55\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173062262547744.jpg\" class=\"aligncenter\" title=\"MySQL \u5173\u8054\u8868\u67e5\u8be2\u7591\u96be\u89e3\u6790\uff1a\u5982\u4f55\u4ece A \u8868\u548c B \u8868\u4e2d\u83b7\u53d6\u7279\u5b9a\u6761\u4ef6\u7684\u8bb0\u5f55\uff1f\u63d2\u56fe\" alt=\"MySQL \u5173\u8054\u8868\u67e5\u8be2\u7591\u96be\u89e3\u6790\uff1a\u5982\u4f55\u4ece A \u8868\u548c B \u8868\u4e2d\u83b7\u53d6\u7279\u5b9a\u6761\u4ef6\u7684\u8bb0\u5f55\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong> \u5173\u8054\u8868\u67e5\u8be2\u7591\u96be\u89e3\u6790<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u4f7f\u7528 mysql \u67e5\u8be2\u4e24\u4e2a\u8868 a \u548c b\uff0c\u4ee5\u63d0\u53d6\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u8bb0\u5f55\uff1f<\/p>\n<p><strong>\u8be6\u7ec6\u8bf4\u660e\uff1a<\/strong><\/p>\n<p>\u8868 a \u7684\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre>a\n---------------------\n| id | outer_id | ... |\n---------------------\n| 1  | 1        | ... |\n| 2  | 1        | ... |\n| 3  | 2        | ... |\n| 4  | 4        | ... |\n| 5  | 6        | ... |\n| 6  | 10       | ... |<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8868 b \u7684\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre>b\n---------------------\n| id | type | ... |\n---------------------\n| 1  | 99   | ... |\n| 2  | 0    | ... |\n| 3  | 99   | ... |\n| 4  | 1    | ... |\n| 5  | 3    | ... |\n| 6  | 99   | ... |<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u9700\u8981\u67e5\u8be2\u51fa\u8868 a \u7684\u4e24\u79cd\u6570\u636e\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528 a \u8868\u7684 outer_id \u5173\u8054 b \u8868\u7684 id\uff0c\u5982\u679c\u5b58\u5728\u5219 type \u4e0d\u80fd\u4e3a 99\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u4fdd\u7559\u8be5\u6570\u636e\u3002<\/li>\n<li>b \u8868\u5b58\u5728\u4e14 type \u7b49\u4e8e 99\u3002<\/li>\n<\/ol>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p><strong>\u67e5\u8be2 1\uff1a<\/strong><\/p>\n<pre>select a.*\nfrom a a\nleft join b b on b.id = a.outer_id\nwhere b.id is null\n   or (b.id is not null and b.type != 99)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u67e5\u8be2 2\uff1a<\/strong><\/p>\n<pre>SELECT a.*\nFROM A a\nINNER JOIN B b ON b.id = a.outer_id AND b.type = 99<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fMySQL \u5173\u8054\u8868\u67e5\u8be2\u7591\u96be\u89e3\u6790\uff1a\u5982\u4f55\u4ece A \u8868\u548c B \u8868\u4e2d\u83b7\u53d6\u7279\u5b9a\u6761\u4ef6\u7684\u8bb0\u5f55\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\u8868\u67e5\u8be2\u7591\u96be\u89e3\u6790 \u95ee\u9898\uff1a \u5982\u4f55\u4f7f\u7528 mysql \u67e5\u8be2\u4e24\u4e2a\u8868 a \u548c b\uff0c\u4ee5\u63d0\u53d6\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u8bb0\u5f55\uff1f \u8be6\u7ec6\u8bf4\u660e\uff1a \u8868 a \u7684\u7ed3\u6784\u5982\u4e0b\uff1a a &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; | id | outer_id | &#8230; | &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; | 1 | 1 | &#8230; | | 2 | 1 | &#8230; | | 3 | 2 | &#8230; | | 4 | 4 | &#8230; | | 5 | 6 | [&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-25496","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25496","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=25496"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25496\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}