{"id":16906,"date":"2025-02-06T10:49:01","date_gmt":"2025-02-06T02:49:01","guid":{"rendered":"https:\/\/92it.top\/?p=16906"},"modified":"2025-02-06T10:49:01","modified_gmt":"2025-02-06T02:49:01","slug":"mysql%e4%b8%ad%ef%bc%8cexists-%e5%92%8c-in-%e7%9a%84%e5%8c%ba%e5%88%ab%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=16906","title":{"rendered":"MySQL\u4e2d\uff0cEXISTS \u548c IN \u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIwNDAyOTI2Nw==&amp;mid=2247502744&amp;idx=1&amp;sn=7040334f5685ed399d04948ca6d39b42&amp;chksm=96c4e7a4a1b36eb250a449605e1ab212ff17acfb7f0ab1809cea1ff5c57ddef1b6e59bb6d0b3&amp;cur_album_id=2818595943724974083&amp;scene=190#rd\">MySQL\u4e2d\uff0cEXISTS \u548c IN \u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/a><\/p>\n\n\n\n<p>\u5728 MySQL \u4e2d\uff0c<code>EXISTS<\/code>&nbsp;\u548c&nbsp;<code>IN<\/code>&nbsp;\u90fd\u7528\u4e8e\u5b50\u67e5\u8be2\uff0c\u5b83\u4eec\u5728\u529f\u80fd\u4e0a\u6709\u4e9b\u76f8\u4f3c\uff0c\u4f46\u5728\u7528\u6cd5\u548c\u6027\u80fd\u4e0a\u5b58\u5728\u4e00\u4e9b\u533a\u522b\u3002\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u5c06\u4ece5\u4e2a\u89d2\u5ea6\u6765\u6df1\u5ea6\u5bf9\u6bd4\u4e24\u8005\u3002<\/p>\n\n\n\n<p><strong>1. \u57fa\u672c\u6982\u5ff5<\/strong>:<\/p>\n\n\n\n<ul>\n<li><code>EXISTS<\/code>\u00a0\u7528\u4e8e\u68c0\u67e5\u5b50\u67e5\u8be2\u662f\u5426\u8fd4\u56de\u81f3\u5c11\u4e00\u884c\u6570\u636e\uff0c\u8fd4\u56de\u5e03\u5c14\u503c\uff08TRUE \u6216 FALSE\uff09\u3002<\/li>\n\n\n\n<li><code>IN<\/code>\u00a0\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u503c\u662f\u5426\u5b58\u5728\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u7684\u7ed3\u679c\u96c6\u5408\u4e2d\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>2. \u8bed\u6cd5<\/strong>:<\/p>\n\n\n\n<ul>\n<li><code>EXISTS<\/code>\u00a0\u901a\u5e38\u4e0e\u4e00\u4e2a\u5b50\u67e5\u8be2\u4e00\u8d77\u4f7f\u7528\uff0c\u6bd4\u5982\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT * FROM table1 WHERE EXISTS (SELECT * FROM table2 WHERE table1.id = table2.id);<br><\/pre>\n\n\n\n<ul>\n<li><code>IN<\/code>\u00a0\u8bed\u6cd5\u4e2d\u901a\u5e38\u76f4\u63a5\u4e0e\u4e00\u4e2a\u5b50\u67e5\u8be2\u6216\u503c\u5217\u8868\u4e00\u8d77\u4f7f\u7528\uff0c\u6bd4\u5982\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT * FROM table1 WHERE table1.id IN (SELECT id FROM table2);<br><\/pre>\n\n\n\n<p><strong>3. \u6027\u80fd<\/strong>:<\/p>\n\n\n\n<ul>\n<li><code>EXISTS<\/code>\u00a0\u901a\u5e38\u5728\u5b50\u67e5\u8be2\u7684\u8fd4\u56de\u7ed3\u679c\u96c6\u5f88\u5927\u65f6\u6027\u80fd\u8f83\u597d\uff0c\u5c24\u5176\u662f\u5f53\u5728\u5b50\u67e5\u8be2\u4e2d\u8fdb\u884c\u5224\u65ad\u540e\uff0c\u53d1\u73b0\u67d0\u4e2a\u6761\u4ef6\u662f\u7b26\u5408\u7684\uff0c\u5219\u4f1a\u7acb\u523b\u8fd4\u56de\u7ed3\u679c\uff0c\u4e0d\u4f1a\u7ee7\u7eed\u67e5\u627e\u6240\u6709\u7684\u884c\u3002<\/li>\n\n\n\n<li><code>IN<\/code>\u00a0\u7684\u6027\u80fd\u5728\u5b50\u67e5\u8be2\u8fd4\u56de\u7684\u7ed3\u679c\u96c6\u8f83\u5c0f\uff0c\u4e14\u503c\u7684\u6bd4\u8f83\u6bd4\u8f83\u7b80\u5355\u65f6\u8868\u73b0\u66f4\u597d\uff0c\u4f46\u5982\u679c\u6709\u5927\u91cf\u7684\u8fd4\u56de\u503c\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>4. NULL \u5904\u7406<\/strong>:<\/p>\n\n\n\n<ul>\n<li>\u4f7f\u7528\u00a0<code>IN<\/code>\u00a0\u65f6\uff0c\u5982\u679c\u5b50\u67e5\u8be2\u8fd4\u56de NULL\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u7ed3\u679c\u3002\u6bd4\u5982\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT * FROM table1 WHERE id IN (1, 2, NULL);<br><\/pre>\n\n\n\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u00a0<code>id<\/code>\u00a0\u5217\u4e2d\u7684\u67d0\u4e2a\u503c\u4e3a NULL\uff0c\u53ef\u80fd\u4e0d\u4f1a\u8fd4\u56de\u4efb\u4f55\u7ed3\u679c\u3002<\/p>\n\n\n\n<ul>\n<li><code>EXISTS<\/code>\u00a0\u4e0d\u53d7 NULL \u7684\u5f71\u54cd\uff0c\u56e0\u4e3a\u5b83\u53ea\u662f\u5224\u65ad\u5b50\u67e5\u8be2\u662f\u5426\u6709\u7ed3\u679c\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>5. \u591a\u91cd\u6761\u4ef6<\/strong>:<\/p>\n\n\n\n<ul>\n<li><code>EXISTS<\/code>\u00a0\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u5904\u7406\u590d\u6742\u7684\u6761\u4ef6\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u67e5\u8be2\u591a\u4e2a\u8868\u5e76\u8fdb\u884c\u590d\u6742\u7684\u8fde\u63a5\u65f6\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p>\u672c\u6587\uff0c\u6211\u4eec\u4ece5\u4e2a\u89d2\u5ea6\u5bf9\u6bd4\u4e86&nbsp;EXISTS&nbsp;\u548c&nbsp;<code>IN\uff0c\u9009\u62e9<\/code><code>EXISTS<\/code><code>\u8fd8\u662f<\/code><code>IN<\/code>&nbsp;\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u67e5\u8be2\u573a\u666f\u3001\u6570\u636e\u91cf\u4ee5\u53ca\u6027\u80fd\u9700\u6c42\u6765\u51b3\u5b9a\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u4f60\u53ea\u5173\u5fc3\u67d0\u4e2a\u6761\u4ef6\u662f\u5426\u6ee1\u8db3\uff08\u5b58\u5728\u81f3\u5c11\u4e00\u884c\uff09\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528&nbsp;<code>EXISTS<\/code>\u3002\u5982\u679c\u4f60\u53ea\u60f3\u5224\u65ad\u67d0\u4e2a\u5217\u7684\u503c\u662f\u5426\u5728\u4e00\u4e2a\u56fa\u5b9a\u96c6\u5408\u4e2d\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528&nbsp;<code>IN<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1aMySQL\u4e2d\uff0cEXISTS \u548c IN \u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f \u5728 MySQL \u4e2d\uff0cEXISTS&nbsp;\u548c&#038;n [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16906"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16906"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16906\/revisions"}],"predecessor-version":[{"id":16907,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16906\/revisions\/16907"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}