{"id":5635,"date":"2024-11-14T08:45:46","date_gmt":"2024-11-14T00:45:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5635\/"},"modified":"2024-11-14T08:45:46","modified_gmt":"2024-11-14T00:45:46","slug":"sql%e6%9f%a5%e8%af%a2exists%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5635\/","title":{"rendered":"sql\u67e5\u8be2exists\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  exists \u64cd\u4f5c\u7b26\u68c0\u67e5\u5b50\u67e5\u8be2\u4e2d\u662f\u5426\u5b58\u5728\u8bb0\u5f55\uff0c\u8fd4\u56de\u5e03\u5c14\u503c true \u6216 false\u3002\u7528\u6cd5\uff1aselect column_list from table_name where exists (subquery); \u4f18\u70b9\uff1a\u6548\u7387\u3001\u6613\u7528\u6027\u548c\u53ef\u8bfb\u6027\u3002\u6ce8\u610f\uff1a\u4e0e where \u5b50\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u901a\u5e38\u7528\u4e8e\u786e\u5b9a\u662f\u5426\u5b58\u5728\u4e0e\u5b50\u67e5\u8be2\u5339\u914d\u7684\u8bb0\u5f55\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053019273448319.jpg\" class=\"aligncenter\" title=\"sql\u67e5\u8be2exists\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"sql\u67e5\u8be2exists\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>EXISTS \u64cd\u4f5c\u7b26\u5728 SQL \u4e2d\u7684\u7528\u6cd5<\/strong><\/p>\n<p>EXISTS \u64cd\u4f5c\u7b26\u5728 SQL \u4e2d\u7528\u4e8e\u68c0\u67e5\u5b50\u67e5\u8be2\u4e2d\u662f\u5426\u5b58\u5728\u8bb0\u5f55\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff1aTrue\uff08\u5b58\u5728\u8bb0\u5f55\uff09\u6216 False\uff08\u4e0d\u5b58\u5728\u8bb0\u5f55\uff09\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>SELECT column_list\nFROM table_name\nWHERE EXISTS (subquery);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>column_list \u8981\u9009\u62e9\u7684\u5217<\/li>\n<li>table_name \u8981\u67e5\u8be2\u7684\u8868<\/li>\n<li>subquery \u4f5c\u4e3a\u6761\u4ef6\u7684\u5b50\u67e5\u8be2<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<p>EXISTS \u64cd\u4f5c\u7b26\u901a\u5e38\u7528\u4e8e\u786e\u5b9a\u8868\u4e2d\u662f\u5426\u5b58\u5728\u4e0e\u5b50\u67e5\u8be2\u5339\u914d\u7684\u8bb0\u5f55\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT *\nFROM customers\nWHERE EXISTS (\n    SELECT *\n    FROM orders\n    WHERE customer_id = customers.id\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u8fd4\u56de\u6240\u6709\u5177\u6709\u81f3\u5c11\u4e00\u4e2a\u8ba2\u5355\u7684\u5ba2\u6237\u3002<\/p>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<p>\u4f7f\u7528 EXISTS \u64cd\u4f5c\u7b26\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li> <strong>\u6548\u7387\uff1a<\/strong>\u5b83\u6bd4 JOIN \u64cd\u4f5c\u7b26\u66f4\u6709\u6548\uff0c\u56e0\u4e3a\u5b83\u53ea\u68c0\u67e5\u5b50\u67e5\u8be2\u4e2d\u662f\u5426\u5b58\u5728\u8bb0\u5f55\uff0c\u800c\u4e0d\u662f\u68c0\u7d22\u6240\u6709\u5339\u914d\u7684\u8bb0\u5f55\u3002<\/li>\n<li> <strong>\u6613\u4e8e\u4f7f\u7528\uff1a<\/strong>\u8bed\u6cd5\u7b80\u5355\u6613\u61c2\u3002<\/li>\n<li> <strong>\u53ef\u8bfb\u6027\uff1a<\/strong>\u53ef\u63d0\u9ad8\u67e5\u8be2\u7684\u53ef\u8bfb\u6027\uff0c\u56e0\u4e3a\u5b83\u660e\u786e\u5730\u6307\u51fa\u6b63\u5728\u68c0\u67e5\u5b50\u67e5\u8be2\u7684\u5e03\u5c14\u6761\u4ef6\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f<\/strong><\/p>\n<ul>\n<li>EXISTS \u64cd\u4f5c\u7b26\u901a\u5e38\u4e0e WHERE \u5b50\u53e5\u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n<li>\u5b50\u67e5\u8be2\u53ef\u4ee5\u5f15\u7528\u4e3b\u67e5\u8be2\u8868\u4e2d\u7684\u5217\uff0c\u4f46\u4e0d\u80fd\u4fee\u6539\u5b83\u4eec\u3002<\/li>\n<li>\u5982\u679c\u5b50\u67e5\u8be2\u8fd4\u56de\u591a\u6761\u8bb0\u5f55\uff0cEXISTS \u64cd\u4f5c\u7b26\u4ecd\u7136\u8fd4\u56de True\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u67e5\u8be2exists\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 \u64cd\u4f5c\u7b26\u68c0\u67e5\u5b50\u67e5\u8be2\u4e2d\u662f\u5426\u5b58\u5728\u8bb0\u5f55\uff0c\u8fd4\u56de\u5e03\u5c14\u503c true \u6216 false\u3002\u7528\u6cd5\uff1aselect column_list from table_name where exists (subquery); \u4f18\u70b9\uff1a\u6548\u7387\u3001\u6613\u7528\u6027\u548c\u53ef\u8bfb\u6027\u3002\u6ce8\u610f\uff1a\u4e0e where \u5b50\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u901a\u5e38\u7528\u4e8e\u786e\u5b9a\u662f\u5426\u5b58\u5728\u4e0e\u5b50\u67e5\u8be2\u5339\u914d\u7684\u8bb0\u5f55\u3002 EXISTS \u64cd\u4f5c\u7b26\u5728 SQL \u4e2d\u7684\u7528\u6cd5 EXISTS \u64cd\u4f5c\u7b26\u5728 SQL \u4e2d\u7528\u4e8e\u68c0\u67e5\u5b50\u67e5\u8be2\u4e2d\u662f\u5426\u5b58\u5728\u8bb0\u5f55\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff1aTrue\uff08\u5b58\u5728\u8bb0\u5f55\uff09\u6216 False\uff08\u4e0d\u5b58\u5728\u8bb0\u5f55\uff09\u3002 \u8bed\u6cd5 SELECT column_list FROM table_name WHERE EXISTS (subquery); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a column_list \u8981\u9009\u62e9\u7684\u5217 table_name \u8981\u67e5\u8be2\u7684\u8868 subquery \u4f5c\u4e3a\u6761\u4ef6\u7684\u5b50\u67e5\u8be2 \u7528\u6cd5 EXISTS \u64cd\u4f5c\u7b26\u901a\u5e38\u7528\u4e8e\u786e\u5b9a\u8868\u4e2d\u662f\u5426\u5b58\u5728\u4e0e\u5b50\u67e5\u8be2\u5339\u914d\u7684\u8bb0\u5f55\u3002\u4f8b\u5982\uff1a SELECT * FROM customers WHERE EXISTS ( SELECT * FROM orders WHERE customer_id [&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-5635","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5635","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=5635"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5635\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}