{"id":200138,"date":"2025-05-06T09:31:37","date_gmt":"2025-05-06T01:31:37","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200138\/"},"modified":"2025-05-06T09:31:37","modified_gmt":"2025-05-06T01:31:37","slug":"sql-%e4%b8%ad%e7%9a%84-in-%e4%b8%8e-exists%ef%bc%9a%e4%ba%86%e8%a7%a3%e6%80%a7%e8%83%bd%e5%92%8c%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200138\/","title":{"rendered":"SQL \u4e2d\u7684 IN \u4e0e EXISTS\uff1a\u4e86\u89e3\u6027\u80fd\u548c\u7528\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>SQL \u4e2d\u7684 IN \u4e0e EXISTS\uff1a\u4e86\u89e3\u6027\u80fd\u548c\u7528\u6cd5<\/h1>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240915\/172639678666e6b972c606a.jpg\" class=\"aligncenter\"><\/p>\n<h3> mysql \u4e2d\u7684 in \u4e0e exists\uff1a\u5b9e\u4f8b\u548c\u63cf\u8ff0 <\/h3>\n<p>\u5728 mysql \u4e2d\uff0cin \u548c exists \u90fd\u7528\u4e8e\u67e5\u8be2\u4e2d\uff0c\u4ee5\u6839\u636e\u5b50\u67e5\u8be2\u4e2d\u662f\u5426\u5b58\u5728\u884c\u6765\u8fc7\u6ee4\u6570\u636e\u3002\u7136\u800c\uff0c\u5b83\u4eec\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0d\u540c\uff0c\u5728\u5b83\u4eec\u4e4b\u95f4\u8fdb\u884c\u9009\u62e9\u4f1a\u5f71\u54cd\u67e5\u8be2\u6027\u80fd\u3002\u8ba9\u6211\u4eec\u901a\u8fc7\u89e3\u91ca\u548c\u5b9e\u8df5\u793a\u4f8b\u6765\u5206\u89e3\u5b83\u4eec\u7684\u5dee\u5f02\u3002<\/p>\n<hr>\n<h3> <strong>1. in \u5b50\u53e5<\/strong> <\/h3>\n<ul>\n<li>\n<p><strong>\u63cf\u8ff0<\/strong>\uff1a<br \/> in \u5b50\u53e5\u7528\u4e8e\u6839\u636e\u5217\u7684\u503c\u662f\u5426\u4e0e\u5217\u8868\u6216\u5b50\u67e5\u8be2\u4e2d\u7684\u4efb\u4f55\u503c\u5339\u914d\u6765\u8fc7\u6ee4\u884c\u3002\u5b83\u68c0\u67e5\u5185\u90e8\u67e5\u8be2\u4e2d\u7684\u5339\u914d\u503c\uff0c\u5e76\u5c06\u5b83\u4eec\u4e0e\u5916\u90e8\u67e5\u8be2\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd<\/strong>\uff1a<br \/> \u5f53\u5b50\u67e5\u8be2\u8fd4\u56de\u5c11\u91cf\u8bb0\u5f55\u65f6\uff0cin \u5b50\u53e5\u901a\u5e38\u5f88\u6709\u6548\u3002\u4f46\u662f\uff0c\u5982\u679c\u5b50\u67e5\u8be2\u8fd4\u56de\u8f83\u5927\u7684\u6570\u636e\u96c6\uff0cin \u53ef\u80fd\u4f1a\u53d8\u6162\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8bed\u6cd5<\/strong>:<\/p>\n<\/li>\n<\/ul>\n<pre>  select columns \n  from table \n  where column in (subquery);\n<\/pre>\n<hr>\n<h3> <strong>2. exists \u5b50\u53e5<\/strong> <\/h3>\n<ul>\n<li>\n<p><strong>\u63cf\u8ff0<\/strong>\uff1a<br \/> exists \u5b50\u53e5\u68c0\u67e5\u5b50\u67e5\u8be2\u8fd4\u56de\u7684\u884c\u662f\u5426\u5b58\u5728\u3002\u5982\u679c\u5b50\u67e5\u8be2\u8fd4\u56de\u4efb\u4f55\u884c\uff0c\u5219 exists \u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a true\uff0c\u5e76\u4e14\u5916\u90e8\u67e5\u8be2\u5c06\u7ee7\u7eed\u8fdb\u884c\u3002\u5b83\u4e0d\u5173\u5fc3\u884c\u7684\u5185\u5bb9\uff0c\u53ea\u5173\u5fc3\u884c\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd<\/strong>\uff1a<br \/> \u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0cexists \u901a\u5e38\u901f\u5ea6\u66f4\u5feb\uff0c\u56e0\u4e3a\u4e00\u65e6\u627e\u5230\u5339\u914d\u9879\uff0c\u5b83\u5c31\u4f1a\u505c\u6b62\u5904\u7406\u3002\u8fd9\u4f7f\u5f97\u5728\u5904\u7406\u8fd4\u56de\u591a\u884c\u7684\u5b50\u67e5\u8be2\u65f6\u53d8\u5f97\u9ad8\u6548\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8bed\u6cd5<\/strong>:<\/p>\n<\/li>\n<\/ul>\n<pre>  select columns \n  from table \n  where exists (subquery);\n<\/pre>\n<hr>\n<h3> <strong>\u5b9e\u8df5\u793a\u4f8b<\/strong> <\/h3>\n<p>\u8ba9\u6211\u4eec\u8003\u8651\u4e24\u4e2a\u8868\uff1a\u5ba2\u6237\u548c\u8ba2\u5355\u3002<\/p>\n<h4> <strong>\u5ba2\u6237\u8868<\/strong>\uff1a <\/h4>\n<table>\n<thead>\n<tr>\n<th>customer_id<\/th>\n<th>customer_name<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>john doe<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>jane smith<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>alice brown<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4> <strong>\u8ba2\u5355\u8868<\/strong>\uff1a <\/h4>\n<table>\n<thead>\n<tr>\n<th>order_id<\/th>\n<th>customer_id<\/th>\n<th>order_total<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>200<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>1<\/td>\n<td>150<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2<\/td>\n<td>300<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6211\u4eec\u5e0c\u671b\u627e\u5230\u6240\u6709\u81f3\u5c11\u4e0b\u8fc7\u4e00\u7b14\u8ba2\u5355\u7684\u5ba2\u6237\u3002<\/p>\n<hr>\n<h3> <strong>\u4f7f\u7528 in \u5b50\u53e5<\/strong> <\/h3>\n<pre>select customer_name \nfrom customers \nwhere customer_id in (select customer_id from orders);\n<\/pre>\n<p><strong>\u8bf4\u660e<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5b50\u67e5\u8be2 (select customer_id from orders) \u8fd4\u56de\u8ba2\u5355\u8868\u4e2d\u51fa\u73b0\u7684\u6240\u6709\u5ba2\u6237 id\u3002<\/li>\n<li>\u5916\u90e8\u67e5\u8be2\u9009\u62e9 customer_id \u5728\u8be5\u7ed3\u679c\u96c6\u4e2d\u7684\u5ba2\u6237\u3002<\/li>\n<\/ul>\n<p><strong>\u7ed3\u679c<\/strong>\uff1a<br \/> |\u5ba2\u6237\u540d\u79f0 |<br \/> |&#8212;&#8212;&#8212;&#8212;&#8211;|<br \/> |\u7ea6\u7ff0\u00b7\u591a\u4f0a |<br \/> |\u7b80\u00b7\u53f2\u5bc6\u65af |<\/p>\n<hr>\n<h3> <strong>\u4f7f\u7528 exists \u5b50\u53e5<\/strong> <\/h3>\n<pre>select customer_name \nfrom customers c\nwhere exists (select 1 from orders o where o.customer_id = c.customer_id);\n<\/pre>\n<p><strong>\u8bf4\u660e<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5b50\u67e5\u8be2 select 1 fromorders o where o.customer_id = c.customer_id \u68c0\u67e5orders\u8868\u4e2d\u7684\u4efb\u4f55\u884c\u662f\u5426\u4e0ecustomers\u8868\u4e2d\u5f53\u524d\u884c\u7684customer_id\u5339\u914d\u3002<\/li>\n<li>\u5982\u679c\u627e\u5230\u4efb\u4f55\u5339\u914d\u9879\uff0cexists \u5c06\u8fd4\u56de true\uff0c\u5e76\u4e14\u8be5\u5ba2\u6237\u5c06\u5305\u542b\u5728\u7ed3\u679c\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>\u7ed3\u679c<\/strong>\uff1a<br \/> |\u5ba2\u6237\u540d\u79f0 |<br \/> |&#8212;&#8212;&#8212;&#8212;&#8211;|<br \/> |\u7ea6\u7ff0\u00b7\u591a\u4f0a |<br \/> |\u7b80\u00b7\u53f2\u5bc6\u65af |<\/p>\n<hr>\n<h3> <strong>\u4e3b\u8981\u5dee\u5f02<\/strong> <\/h3>\n<ol>\n<li>\n<p><strong>\u8fd4\u56de\u503c<\/strong>:<\/p>\n<ul>\n<li> in\uff1a\u5c06\u5217\u7684\u503c\u4e0e\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\u8fdb\u884c\u6bd4\u8f83\u3002<\/li>\n<li> exists\uff1a\u6839\u636e\u5b50\u67e5\u8be2\u662f\u5426\u8fd4\u56de\u4efb\u4f55\u884c\uff0c\u8fd4\u56de true \u6216 false\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6548\u7387<\/strong>\uff1a<\/p>\n<ul>\n<li> in \u5bf9\u4e8e\u8f83\u5c0f\u7684\u6570\u636e\u96c6\u66f4\u6709\u6548\u3002<\/li>\n<li> \u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0cexists \u901f\u5ea6\u66f4\u5feb\uff0c\u5c24\u5176\u662f\u5f53\u5b50\u67e5\u8be2\u8fd4\u56de\u8bb8\u591a\u884c\u65f6\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7528\u4f8b<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5c06\u5217\u7684\u503c\u4e0e\u4e00\u5c0f\u90e8\u5206\u53ef\u80fd\u503c\u8fdb\u884c\u6bd4\u8f83\u65f6\u4f7f\u7528 in\u3002<\/li>\n<li>\u5f53\u60a8\u68c0\u67e5\u5b50\u67e5\u8be2\u4e2d\u662f\u5426\u5b58\u5728\u884c\u65f6\uff08\u4f8b\u5982\uff0c\u5f53\u5916\u90e8\u67e5\u8be2\u548c\u5185\u90e8\u67e5\u8be2\u4e4b\u95f4\u5b58\u5728\u76f8\u5173\u6027\u65f6\uff09\uff0c\u8bf7\u4f7f\u7528 exists\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<hr>\n<h3> <strong>\u6027\u80fd\u793a\u4f8b<\/strong> <\/h3>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\uff1a<\/p>\n<ul>\n<li><strong>10,000 \u540d\u5ba2\u6237<\/strong><\/li>\n<li><strong>100,000 \u4efd\u8ba2\u5355<\/strong><\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528 in \u67e5\u8be2<\/strong>:<\/p>\n<pre>select customer_name \nfrom customers \nwhere customer_id in (select customer_id from orders);\n<\/pre>\n<ul>\n<li> <strong>\u6267\u884c<\/strong>\uff1amysql\u5c06\u4ece\u5b50\u67e5\u8be2\u4e2d\u68c0\u7d22\u6574\u4e2a\u7ed3\u679c\u96c6\uff0c\u5e76\u5c06\u5176\u4e0e\u5916\u90e8\u67e5\u8be2\u4e2d\u7684\u6bcf\u4e00\u884c\u8fdb\u884c\u6bd4\u8f83\u3002<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528 exists \u67e5\u8be2<\/strong>:<\/p>\n<pre>select customer_name \nfrom customers c\nwhere exists (select 1 from orders o where o.customer_id = c.customer_id);\n<\/pre>\n<ul>\n<li> <strong>\u6267\u884c<\/strong>\uff1amysql \u5c06\u68c0\u67e5\u5916\u90e8\u67e5\u8be2\u4e2d\u7684\u6bcf\u4e00\u884c\uff0c\u5e76\u5728\u5b50\u67e5\u8be2\u4e2d\u627e\u5230\u5339\u914d\u7684\u884c\u540e\u505c\u6b62\uff0c\u4ece\u800c\u4f7f\u5927\u578b\u6570\u636e\u96c6\u7684\u901f\u5ea6\u66f4\u5feb\u3002<\/li>\n<\/ul>\n<hr>\n<h3> <strong>\u7ed3\u8bba<\/strong> <\/h3>\n<ul>\n<li> <strong>\u5f53\u60a8\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u5217\u8868\u8981\u6bd4\u8f83\u6216\u4e00\u4e2a\u5c0f\u7684\u5b50\u67e5\u8be2\u7ed3\u679c\u65f6\uff0c\u8bf7\u4f7f\u7528 in<\/strong>\u3002<\/li>\n<li> <strong>\u5f53\u60a8\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u6216\u9700\u8981\u68c0\u67e5\u5b50\u67e5\u8be2\u4e2d\u662f\u5426\u5b58\u5728\u76f8\u5173\u6570\u636e\u65f6\uff0c\u8bf7\u4f7f\u7528 exists<\/strong>\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6570\u636e\u5e93\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664 <\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>SQL \u4e2d\u7684 IN \u4e0e EXIS&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200138","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200138"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200138\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}