{"id":25427,"date":"2024-11-24T11:44:12","date_gmt":"2024-11-24T03:44:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25427\/"},"modified":"2024-11-24T11:44:12","modified_gmt":"2024-11-24T03:44:12","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%85%b3%e8%81%94%e8%a1%a8%e6%9f%a5%e8%af%a2%e4%b8%a4%e7%a7%8d%e7%b1%bb%e5%9e%8b%e7%9a%84%e6%95%b0%e6%8d%ae%ef%bc%9a%e5%85%b3%e8%81%94%e8%a1%a8%e6%9f%a5%e8%af%a2-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25427\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u5173\u8054\u8868\u67e5\u8be2\u4e24\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff1a\u5173\u8054\u8868\u67e5\u8be2\u6280\u5de7\u53ca\u4f18\u5316\u8be6\u89e3"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173061054493234.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528\u5173\u8054\u8868\u67e5\u8be2\u4e24\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff1a\u5173\u8054\u8868\u67e5\u8be2\u6280\u5de7\u53ca\u4f18\u5316\u8be6\u89e3\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528\u5173\u8054\u8868\u67e5\u8be2\u4e24\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff1a\u5173\u8054\u8868\u67e5\u8be2\u6280\u5de7\u53ca\u4f18\u5316\u8be6\u89e3\u63d2\u56fe\" \/><\/p>\n<p><strong> \u5173\u8054\u8868\u67e5\u8be2\u8be6\u89e3<\/strong><\/p>\n<p>\u6709\u5173\u5173\u8054\u8868\u67e5\u8be2\u7684\u7591\u60d1\uff0c\u662f\u6570\u636e\u5e93\u5f00\u53d1\u4e2d\u4e00\u4e2a\u5e38\u89c1\u7684\u6311\u6218\u3002\u672c\u6587\u5c06\u89e3\u6790\u4e00\u4e2a\u590d\u6742\u7684\u67e5\u8be2\uff0c\u4ee5\u9610\u660e\u5982\u4f55\u4f7f\u7528\u5173\u8054\u8868\u68c0\u7d22\u6240\u9700\u6570\u636e\u3002<\/p>\n<p>\u95ee\u9898\u63cf\u8ff0\uff1a<\/p>\n<p>\u6709\u5982\u4e0b\u4e24\u4e2a\u8868\uff1a<\/p>\n<ul>\n<li>a \u8868\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1aid \u548c outer_id<\/li>\n<li>b \u8868\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1aid\u3001type<\/li>\n<\/ul>\n<p>\u76ee\u7684\uff1a\u67e5\u8be2\u4e24\u79cd\u7c7b\u578b\u7684 a \u8868\u6570\u636e\u3002\u4e00\u79cd\u662f\u4f7f\u7528 a \u8868\u7684 outer_id \u5173\u8054\u5230 b \u8868\u4e2d\u5b58\u5728\u7684 id\uff0c\u4e14 b \u8868\u7684 type \u4e0d\u7b49\u4e8e 99\u3002\u53e6\u4e00\u7c7b\u578b\u662f b \u8868\u5fc5\u987b\u5b58\u5728\u4e14 type \u7b49\u4e8e 99\u3002<\/p>\n<p>\u539f\u59cb\u67e5\u8be2\u5c1d\u8bd5\uff1a<\/p>\n<pre>select a.*\nfrom a as a\nwhere ((select b.type from b as b where b.id = a.outer_id limit 1) is null)\nor (select b.type from b as b where b.id = a.outer_id limit 1) != 99<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>select a.*\nfrom a as a\nwhere (select b.type from b as b where b.id = a.outer_id limit 1) = 99<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f18\u5316\u67e5\u8be2\uff1a<\/p>\n<p>\u6839\u636e\u63d0\u4f9b\u7684\u8981\u6c42\uff0c\u5df2\u4f18\u5316\u67e5\u8be2\u4e3a\uff1a<\/p>\n<p>\u7b2c\u4e00\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff1a<\/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>\u7b2c\u4e8c\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff1a<\/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>\u5728\u4f18\u5316\u540e\u7684\u67e5\u8be2\u4e2d\uff1a<\/p>\n<ul>\n<li>\u5de6\u8fde\u63a5 (left join) \u7528\u4e8e\u83b7\u53d6\u7b2c\u4e00\u79cd\u6570\u636e\uff0c\u5176\u4e2d b \u8868\u53ef\u80fd\u4e0d\u5b58\u5728\u6216\u8005 type \u4e0d\u7b49\u4e8e 99\u3002<\/li>\n<li>\u5185\u8fde\u63a5 (inner join) \u7528\u4e8e\u83b7\u53d6\u7b2c\u4e8c\u79cd\u6570\u636e\uff0c\u5176\u4e2d b \u8868\u5fc5\u987b\u5b58\u5728\u4e14 type \u7b49\u4e8e 99\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528\u5173\u8054\u8868\u67e5\u8be2\u4e24\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff1a\u5173\u8054\u8868\u67e5\u8be2\u6280\u5de7\u53ca\u4f18\u5316\u8be6\u89e3\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\u8be6\u89e3 \u6709\u5173\u5173\u8054\u8868\u67e5\u8be2\u7684\u7591\u60d1\uff0c\u662f\u6570\u636e\u5e93\u5f00\u53d1\u4e2d\u4e00\u4e2a\u5e38\u89c1\u7684\u6311\u6218\u3002\u672c\u6587\u5c06\u89e3\u6790\u4e00\u4e2a\u590d\u6742\u7684\u67e5\u8be2\uff0c\u4ee5\u9610\u660e\u5982\u4f55\u4f7f\u7528\u5173\u8054\u8868\u68c0\u7d22\u6240\u9700\u6570\u636e\u3002 \u95ee\u9898\u63cf\u8ff0\uff1a \u6709\u5982\u4e0b\u4e24\u4e2a\u8868\uff1a a \u8868\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1aid \u548c outer_id b \u8868\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1aid\u3001type \u76ee\u7684\uff1a\u67e5\u8be2\u4e24\u79cd\u7c7b\u578b\u7684 a \u8868\u6570\u636e\u3002\u4e00\u79cd\u662f\u4f7f\u7528 a \u8868\u7684 outer_id \u5173\u8054\u5230 b \u8868\u4e2d\u5b58\u5728\u7684 id\uff0c\u4e14 b \u8868\u7684 type \u4e0d\u7b49\u4e8e 99\u3002\u53e6\u4e00\u7c7b\u578b\u662f b \u8868\u5fc5\u987b\u5b58\u5728\u4e14 type \u7b49\u4e8e 99\u3002 \u539f\u59cb\u67e5\u8be2\u5c1d\u8bd5\uff1a select a.* from a as a where ((select b.type from b as b where b.id = a.outer_id limit 1) is null) or (select b.type [&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-25427","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25427","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=25427"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25427\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}