{"id":5230,"date":"2024-11-14T10:15:26","date_gmt":"2024-11-14T02:15:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5230\/"},"modified":"2024-11-14T10:15:26","modified_gmt":"2024-11-14T02:15:26","slug":"mysql%e4%b8%adin%e6%80%8e%e4%b9%88%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5230\/","title":{"rendered":"mysql\u4e2din\u600e\u4e48\u4f18\u5316"},"content":{"rendered":"<blockquote><p>\n  mysql in \u67e5\u8be2\u4f18\u5316\u65b9\u6cd5\uff1a\u521b\u5efa\u7d22\u5f15\u3002\u4f7f\u7528 exists \u5b50\u67e5\u8be2\u3002\u4f7f\u7528 union all \u67e5\u8be2\u3002\u4f7f\u7528\u4e34\u65f6\u8868\u3002\u4f7f\u7528 case \u8868\u8fbe\u5f0f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/26\/2024052617062458160.jpg\" class=\"aligncenter\" title=\"mysql\u4e2din\u600e\u4e48\u4f18\u5316\u63d2\u56fe\" alt=\"mysql\u4e2din\u600e\u4e48\u4f18\u5316\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u4e2d IN \u67e5\u8be2\u4f18\u5316<\/strong><\/p>\n<p>IN \u67e5\u8be2\u662f MySQL \u4e2d\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u503c\u662f\u5426\u5c5e\u4e8e\u4e00\u7ec4\u503c\u7684\u4e00\u79cd\u67e5\u8be2\u7c7b\u578b\u3002\u7136\u800c\uff0c\u5f53 IN \u5b50\u53e5\u5305\u542b\u5927\u91cf\u503c\u65f6\uff0c\u67e5\u8be2\u6027\u80fd\u53ef\u80fd\u4f1a\u4e0b\u964d\u3002<\/p>\n<p><strong>\u4f18\u5316 IN \u67e5\u8be2\u7684\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p><strong>1. \u4f7f\u7528\u7d22\u5f15\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728 IN \u5b50\u53e5\u4e2d\u6d89\u53ca\u7684\u5217\u4e0a\u521b\u5efa\u7d22\u5f15\u3002\u8fd9\u5c06\u5141\u8bb8 MySQL \u5feb\u901f\u67e5\u627e\u5339\u914d\u7684\u503c\uff0c\u4ece\u800c\u63d0\u9ad8\u901f\u5ea6\u3002<\/li>\n<\/ul>\n<p><strong>2. \u4f7f\u7528 EXISTS \u5b50\u67e5\u8be2\uff1a<\/strong><\/p>\n<ul>\n<li>\u5c06 IN \u5b50\u53e5\u66ff\u6362\u4e3a EXISTS \u5b50\u67e5\u8be2\u3002\u8fd9\u5c06\u5f3a\u5236 MySQL \u9010\u884c\u68c0\u67e5\u8868\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u68c0\u67e5\u6240\u6709\u503c\uff0c\u4ece\u800c\u53ef\u80fd\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><strong>3. \u4f7f\u7528 UNION ALL\uff1a<\/strong><\/p>\n<ul>\n<li>\u5c06 IN \u5b50\u53e5\u62c6\u5206\u4e3a\u591a\u4e2a\u5e26\u6709\u5355\u503c IN \u5b50\u53e5\u7684 UNION ALL \u67e5\u8be2\u3002\u8fd9\u5c06\u5f3a\u5236 MySQL \u591a\u6b21\u6267\u884c\u67e5\u8be2\uff0c\u4f46\u53ef\u80fd\u6bd4\u5355\u4e00 IN \u67e5\u8be2\u66f4\u5feb\u3002<\/li>\n<\/ul>\n<p><strong>4. \u4f7f\u7528\u4e34\u65f6\u8868\uff1a<\/strong><\/p>\n<ul>\n<li>\u5c06 IN \u5b50\u53e5\u4e2d\u7684\u503c\u5b58\u50a8\u5728\u4e34\u65f6\u8868\u4e2d\u3002\u7136\u540e\uff0c\u5728\u4e3b\u67e5\u8be2\u4e2d\u4f7f\u7528 JOIN \u8fd0\u7b97\u7b26\u8fde\u63a5\u4e3b\u8868\u4e0e\u4e34\u65f6\u8868\uff0c\u4ece\u800c\u907f\u514d\u5728\u5927\u8868\u4e2d\u8fdb\u884c IN \u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><strong>5. \u4f7f\u7528 CASE \u8868\u8fbe\u5f0f\uff1a<\/strong><\/p>\n<ul>\n<li>\u5bf9\u4e8e\u8f83\u5c0f\u7684 IN \u5b50\u53e5\uff0c\u53ef\u4ee5\u4f7f\u7528 CASE \u8868\u8fbe\u5f0f\u6765\u68c0\u67e5\u503c\u3002\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\uff0c\u5176\u4e2d\u5305\u542b\u5339\u914d\u7684\u503c\uff0c\u4ece\u800c\u907f\u514d\u4e86 IN \u67e5\u8be2\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><strong>\u539f\u59cb IN \u67e5\u8be2\uff1a<\/strong><\/p>\n<pre>SELECT * FROM table WHERE id IN (1, 2, 3, 4, 5, ...);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u7d22\u5f15\u7684 IN \u67e5\u8be2\uff1a<\/strong><\/p>\n<pre>SELECT * FROM table WHERE id IN (1, 2, 3, 4, 5, ...) USE INDEX (id);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 EXISTS \u5b50\u67e5\u8be2\u7684\u67e5\u8be2\uff1a<\/strong><\/p>\n<pre>SELECT * FROM table WHERE EXISTS (SELECT 1 FROM subquery WHERE subquery.id = table.id);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e2din\u600e\u4e48\u4f18\u5316\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>mysql in \u67e5\u8be2\u4f18\u5316\u65b9\u6cd5\uff1a\u521b\u5efa\u7d22\u5f15\u3002\u4f7f\u7528 exists \u5b50\u67e5\u8be2\u3002\u4f7f\u7528 union all \u67e5\u8be2\u3002\u4f7f\u7528\u4e34\u65f6\u8868\u3002\u4f7f\u7528 case \u8868\u8fbe\u5f0f\u3002 MySQL \u4e2d IN \u67e5\u8be2\u4f18\u5316 IN \u67e5\u8be2\u662f MySQL \u4e2d\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u503c\u662f\u5426\u5c5e\u4e8e\u4e00\u7ec4\u503c\u7684\u4e00\u79cd\u67e5\u8be2\u7c7b\u578b\u3002\u7136\u800c\uff0c\u5f53 IN \u5b50\u53e5\u5305\u542b\u5927\u91cf\u503c\u65f6\uff0c\u67e5\u8be2\u6027\u80fd\u53ef\u80fd\u4f1a\u4e0b\u964d\u3002 \u4f18\u5316 IN \u67e5\u8be2\u7684\u65b9\u6cd5\uff1a 1. \u4f7f\u7528\u7d22\u5f15\uff1a \u5728 IN \u5b50\u53e5\u4e2d\u6d89\u53ca\u7684\u5217\u4e0a\u521b\u5efa\u7d22\u5f15\u3002\u8fd9\u5c06\u5141\u8bb8 MySQL \u5feb\u901f\u67e5\u627e\u5339\u914d\u7684\u503c\uff0c\u4ece\u800c\u63d0\u9ad8\u901f\u5ea6\u3002 2. \u4f7f\u7528 EXISTS \u5b50\u67e5\u8be2\uff1a \u5c06 IN \u5b50\u53e5\u66ff\u6362\u4e3a EXISTS \u5b50\u67e5\u8be2\u3002\u8fd9\u5c06\u5f3a\u5236 MySQL \u9010\u884c\u68c0\u67e5\u8868\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u68c0\u67e5\u6240\u6709\u503c\uff0c\u4ece\u800c\u53ef\u80fd\u63d0\u9ad8\u6027\u80fd\u3002 3. \u4f7f\u7528 UNION ALL\uff1a \u5c06 IN \u5b50\u53e5\u62c6\u5206\u4e3a\u591a\u4e2a\u5e26\u6709\u5355\u503c IN \u5b50\u53e5\u7684 UNION ALL \u67e5\u8be2\u3002\u8fd9\u5c06\u5f3a\u5236 MySQL \u591a\u6b21\u6267\u884c\u67e5\u8be2\uff0c\u4f46\u53ef\u80fd\u6bd4\u5355\u4e00 [&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-5230","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5230","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=5230"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5230\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}