{"id":5787,"date":"2024-11-14T08:39:03","date_gmt":"2024-11-14T00:39:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5787\/"},"modified":"2024-11-14T08:39:03","modified_gmt":"2024-11-14T00:39:03","slug":"mysql-exists%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5787\/","title":{"rendered":"mysql\u00a0exists\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  exists \u7528\u6cd5\uff1a\u68c0\u67e5\u5b50\u67e5\u8be2\u4e2d\u662f\u5426\u5b58\u5728\u8bb0\u5f55\uff0c\u5b58\u5728\u65f6\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\u3002\u8bed\u6cd5\uff1aselect * from table_name where exists (subquery);\u5b50\u67e5\u8be2\u5fc5\u987b\u8fd4\u56de\u5e03\u5c14\u503c\u3002exists \u8fd0\u7b97\u7b26\u907f\u514d\u4e0d\u5fc5\u8981\u7684 join \u64cd\u4f5c\uff0c\u4f18\u5316\u67e5\u8be2\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/02\/2024060201302327121.jpg\" class=\"aligncenter\" title=\"mysql\u00a0exists\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"mysql\u00a0exists\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL EXISTS \u7528\u6cd5<\/strong><\/p>\n<p>EXISTS \u8fd0\u7b97\u7b26\u7528\u4e8e\u68c0\u67e5\u5b50\u67e5\u8be2\u4e2d\u662f\u5426\u5b58\u5728\u4efb\u4f55\u8bb0\u5f55\u3002\u5982\u679c\u5b58\u5728\u8bb0\u5f55\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>SELECT * FROM table_name\nWHERE EXISTS (subquery);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0csubquery \u662f\u8fd4\u56de\u5e03\u5c14\u503c\u7684\u5b50\u67e5\u8be2\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a orders \u8868\uff0c\u5305\u542b\u4ee5\u4e0b\u5217\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>order_id<\/th>\n<th>product_name<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>&#8220;Apple&#8221;<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>&#8220;Orange&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8981\u67e5\u627e\u662f\u5426\u5b58\u5728\u8ba2\u5355\u5305\u542b\u4ea7\u54c1\u540d\u79f0\u4e3a &#8220;Banana&#8221; \u7684\u8bb0\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT * FROM orders\nWHERE EXISTS (SELECT * FROM orders WHERE product_name = \"Banana\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7ed3\u679c\u96c6\uff0c\u56e0\u4e3a\u6ca1\u6709\u8ba2\u5355\u5305\u542b\u4ea7\u54c1\u540d\u79f0\u4e3a &#8220;Banana&#8221; \u7684\u8bb0\u5f55\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>EXISTS \u8fd0\u7b97\u7b26\u4ec5\u68c0\u67e5\u662f\u5426\u5b58\u5728\u8bb0\u5f55\uff0c\u800c\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\u3002<\/li>\n<li>\u5b50\u67e5\u8be2\u5fc5\u987b\u8fd4\u56de\u5e03\u5c14\u503c (true \u6216 false)\u3002<\/li>\n<li>EXISTS \u8fd0\u7b97\u7b26\u5728\u4f18\u5316\u67e5\u8be2\u65b9\u9762\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684 JOIN \u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f&nbsp;exists\u600e\u4e48\u7528\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>exists \u7528\u6cd5\uff1a\u68c0\u67e5\u5b50\u67e5\u8be2\u4e2d\u662f\u5426\u5b58\u5728\u8bb0\u5f55\uff0c\u5b58\u5728\u65f6\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\u3002\u8bed\u6cd5\uff1aselect * from table_name where exists (subquery);\u5b50\u67e5\u8be2\u5fc5\u987b\u8fd4\u56de\u5e03\u5c14\u503c\u3002exists \u8fd0\u7b97\u7b26\u907f\u514d\u4e0d\u5fc5\u8981\u7684 join \u64cd\u4f5c\uff0c\u4f18\u5316\u67e5\u8be2\u3002 MySQL EXISTS \u7528\u6cd5 EXISTS \u8fd0\u7b97\u7b26\u7528\u4e8e\u68c0\u67e5\u5b50\u67e5\u8be2\u4e2d\u662f\u5426\u5b58\u5728\u4efb\u4f55\u8bb0\u5f55\u3002\u5982\u679c\u5b58\u5728\u8bb0\u5f55\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\u3002 \u8bed\u6cd5 SELECT * FROM table_name WHERE EXISTS (subquery); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0csubquery \u662f\u8fd4\u56de\u5e03\u5c14\u503c\u7684\u5b50\u67e5\u8be2\u3002 \u793a\u4f8b \u5047\u8bbe\u6709\u4e00\u4e2a orders \u8868\uff0c\u5305\u542b\u4ee5\u4e0b\u5217\uff1a order_id product_name 1 &#8220;Apple&#8221; 2 &#8220;Orange&#8221; \u8981\u67e5\u627e\u662f\u5426\u5b58\u5728\u8ba2\u5355\u5305\u542b\u4ea7\u54c1\u540d\u79f0\u4e3a &#8220;Banana&#8221; \u7684\u8bb0\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a SELECT * FROM orders WHERE EXISTS (SELECT * FROM orders WHERE [&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-5787","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5787","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=5787"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5787\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}