{"id":1733,"date":"2025-02-16T09:05:30","date_gmt":"2025-02-16T01:05:30","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1733"},"modified":"2025-02-16T09:05:30","modified_gmt":"2025-02-16T01:05:30","slug":"sql-exists","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/16\/sql-exists\/","title":{"rendered":"SQL EXISTS\u00a0"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>EXISTS<\/code> \u662f SQL \u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u5b57\uff0c\u7528\u4e8e\u6d4b\u8bd5\u5b50\u67e5\u8be2\u662f\u5426\u8fd4\u56de\u81f3\u5c11\u4e00\u884c\u7ed3\u679c\u3002<code>EXISTS<\/code> \u5e38\u7528\u4e8e\u5728\u67e5\u8be2\u4e2d\u68c0\u67e5\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u662f\u5426\u5b58\u5728\uff0c\u901a\u5e38\u4e0e <code>SELECT<\/code> \u5b50\u53e5\u4e2d\u7684 <code>WHERE<\/code> \u6761\u4ef6\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong><code>EXISTS<\/code> \u7684\u57fa\u672c\u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2\nFROM table_name\nWHERE EXISTS (subquery);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>subquery<\/code>\uff1a\u4e00\u4e2a\u8fd4\u56de\u5e03\u5c14\u503c\u7684\u5b50\u67e5\u8be2\u3002\u5b50\u67e5\u8be2\u7528\u4e8e\u68c0\u67e5\u662f\u5426\u5b58\u5728\u6ee1\u8db3\u6761\u4ef6\u7684\u8bb0\u5f55\u3002<\/li>\n\n\n\n<li><code>EXISTS<\/code> \u4f1a\u8fd4\u56de\u5e03\u5c14\u503c <code>TRUE<\/code> \u6216 <code>FALSE<\/code>\uff0c\u8868\u793a\u5b50\u67e5\u8be2\u662f\u5426\u8fd4\u56de\u4e86\u81f3\u5c11\u4e00\u884c\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>EXISTS<\/code> \u5b50\u67e5\u8be2\u7684\u5de5\u4f5c\u539f\u7406<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>EXISTS<\/code> \u8bed\u53e5\u5728\u5b50\u67e5\u8be2\u4e2d\u67e5\u627e\u6570\u636e\uff0c\u4e14\u4ec5\u5173\u6ce8\u5b50\u67e5\u8be2\u662f\u5426\u81f3\u5c11\u8fd4\u56de\u4e00\u884c\u6570\u636e\u3002\u53ea\u8981\u5b50\u67e5\u8be2\u8fd4\u56de\u4efb\u4f55\u7ed3\u679c\uff0c<code>EXISTS<\/code> \u5c31\u4f1a\u8fd4\u56de <code>TRUE<\/code>\uff0c\u5426\u5219\u8fd4\u56de <code>FALSE<\/code>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u5b50\u67e5\u8be2\u8fd4\u56de\u81f3\u5c11\u4e00\u884c\u6570\u636e\uff0c<code>EXISTS<\/code> \u7684\u6761\u4ef6\u4e3a <code>TRUE<\/code>\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u5b50\u67e5\u8be2\u8fd4\u56de\u96f6\u884c\u6570\u636e\uff0c<code>EXISTS<\/code> \u7684\u6761\u4ef6\u4e3a <code>FALSE<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong><code>EXISTS<\/code> \u7684\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6709\u4e24\u4e2a\u8868\uff1a<code>employees<\/code> \u548c <code>departments<\/code>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>employees<\/code> \u8868\u5305\u542b\u5458\u5de5\u4fe1\u606f\uff1a<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>emp_id<\/th><th>emp_name<\/th><th>dept_id<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Alice<\/td><td>10<\/td><\/tr><tr><td>2<\/td><td>Bob<\/td><td>20<\/td><\/tr><tr><td>3<\/td><td>Charlie<\/td><td>10<\/td><\/tr><tr><td>4<\/td><td>David<\/td><td>30<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>departments<\/code> \u8868\u5305\u542b\u90e8\u95e8\u4fe1\u606f\uff1a<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>dept_id<\/th><th>dept_name<\/th><\/tr><\/thead><tbody><tr><td>10<\/td><td>HR<\/td><\/tr><tr><td>20<\/td><td>IT<\/td><\/tr><tr><td>30<\/td><td>Finance<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 <strong>\u4f7f\u7528 <code>EXISTS<\/code> \u67e5\u627e\u6709\u90e8\u95e8\u7684\u5458\u5de5<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u67e5\u8be2\u6240\u6709\u6709\u90e8\u95e8\u7684\u5458\u5de5\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>EXISTS<\/code> \u6765\u68c0\u67e5\u6bcf\u4e2a\u5458\u5de5\u662f\u5426\u6709\u76f8\u5e94\u7684\u90e8\u95e8\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u90a3\u4e9b\u5c5e\u4e8e\u90e8\u95e8 <code>HR<\/code> \u7684\u5458\u5de5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_name\nFROM employees e\nWHERE EXISTS (\n    SELECT 1\n    FROM departments d\n    WHERE d.dept_id = e.dept_id AND d.dept_name = 'HR'\n);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>emp_name<\/th><\/tr><\/thead><tbody><tr><td>Alice<\/td><\/tr><tr><td>Charlie<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>EXISTS<\/code> \u5b50\u67e5\u8be2\u68c0\u67e5 <code>employees<\/code> \u8868\u4e2d\u7684\u6bcf\u4e2a <code>emp_id<\/code> \u662f\u5426\u4e0e <code>departments<\/code> \u8868\u4e2d\u7684\u90e8\u95e8 ID \u5339\u914d\uff0c\u5e76\u4e14\u90e8\u95e8\u540d\u79f0\u4e3a <code>'HR'<\/code>\u3002\u5f53 <code>EXISTS<\/code> \u5b50\u67e5\u8be2\u8fd4\u56de\u7ed3\u679c\u65f6\uff0c<code>emp_name<\/code> \u88ab\u8fd4\u56de\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 <strong><code>EXISTS<\/code> \u67e5\u627e\u65e0\u90e8\u95e8\u7684\u5458\u5de5<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>EXISTS<\/code> \u6765\u67e5\u627e\u6ca1\u6709\u5339\u914d\u90e8\u95e8\u7684\u5458\u5de5\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u6ca1\u6709\u90e8\u95e8\u4fe1\u606f\u7684\u5458\u5de5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_name\nFROM employees e\nWHERE NOT EXISTS (\n    SELECT 1\n    FROM departments d\n    WHERE d.dept_id = e.dept_id\n);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>emp_name<\/th><\/tr><\/thead><tbody><tr><td>David<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\uff0c<code>NOT EXISTS<\/code> \u68c0\u67e5\u662f\u5426\u6ca1\u6709\u4e0e <code>employees<\/code> \u8868\u4e2d\u7684 <code>emp_id<\/code> \u5339\u914d\u7684\u90e8\u95e8\u8bb0\u5f55\u3002\u5f53\u6ca1\u6709\u8fd4\u56de\u5339\u914d\u8bb0\u5f55\u65f6\uff0c\u5458\u5de5\u7684\u59d3\u540d\u4f1a\u88ab\u8fd4\u56de\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong><code>EXISTS<\/code> \u4e0e <code>IN<\/code> \u7684\u6bd4\u8f83<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>EXISTS<\/code> \u548c <code>IN<\/code> \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u76f8\u4f3c\uff0c\u90fd\u7528\u4e8e\u68c0\u67e5\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u3002\u4f46\u662f\uff0c\u5b83\u4eec\u4e4b\u95f4\u6709\u4e00\u4e9b\u7ec6\u5fae\u7684\u533a\u522b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>EXISTS<\/code><\/strong>\uff1a\u901a\u5e38\u7528\u4e8e\u68c0\u67e5\u5b50\u67e5\u8be2\u662f\u5426\u8fd4\u56de\u4efb\u4f55\u8bb0\u5f55\uff08\u53ea\u5173\u5fc3\u662f\u5426\u6709\u7ed3\u679c\uff09\u3002\u901a\u5e38\u4e0e <code>SELECT 1<\/code> \u6216 <code>SELECT *<\/code> \u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong><code>IN<\/code><\/strong>\uff1a\u901a\u5e38\u7528\u4e8e\u68c0\u67e5\u67d0\u4e2a\u503c\u662f\u5426\u5305\u542b\u5728\u5b50\u67e5\u8be2\u8fd4\u56de\u7684\u7ed3\u679c\u96c6\u4e2d\u3002<code>IN<\/code> \u4f1a\u8fd4\u56de\u5b50\u67e5\u8be2\u4e2d\u7684\u6240\u6709\u503c\uff0c\u8fdb\u884c\u6bd4\u8f83\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4.1 <strong><code>EXISTS<\/code> \u793a\u4f8b<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_name\nFROM employees e\nWHERE EXISTS (\n    SELECT 1\n    FROM departments d\n    WHERE d.dept_id = e.dept_id\n);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.2 <strong><code>IN<\/code> \u793a\u4f8b<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_name\nFROM employees e\nWHERE e.dept_id IN (\n    SELECT dept_id\n    FROM departments\n);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u533a\u522b<\/strong>\uff1a<code>EXISTS<\/code> \u53ea\u5173\u5fc3\u5b50\u67e5\u8be2\u662f\u5426\u8fd4\u56de\u81f3\u5c11\u4e00\u884c\u6570\u636e\uff0c\u800c <code>IN<\/code> \u4f1a\u8fd4\u56de\u5b50\u67e5\u8be2\u4e2d\u7684\u6240\u6709\u503c\u5e76\u8fdb\u884c\u6bd4\u8f83\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong><code>EXISTS<\/code> \u5b50\u67e5\u8be2\u7684\u6027\u80fd<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>EXISTS<\/code> \u5b50\u67e5\u8be2\u901a\u5e38\u6bd4 <code>IN<\/code> \u66f4\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u3002\u8fd9\u662f\u56e0\u4e3a <code>EXISTS<\/code> \u53ea\u9700\u8981\u68c0\u67e5\u5b50\u67e5\u8be2\u662f\u5426\u8fd4\u56de\u4efb\u4f55\u6570\u636e\uff0c\u800c <code>IN<\/code> \u9700\u8981\u8fd4\u56de\u6240\u6709\u5339\u914d\u503c\uff0c\u5e76\u4e0e\u5916\u90e8\u67e5\u8be2\u7684\u6bcf\u884c\u8fdb\u884c\u6bd4\u8f83\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u5916\u90e8\u67e5\u8be2\u548c\u5b50\u67e5\u8be2\u90fd\u6d89\u53ca\u5927\u91cf\u6570\u636e\uff0c<code>EXISTS<\/code> \u53ef\u80fd\u4f1a\u66f4\u5feb\u901f\uff0c\u56e0\u4e3a\u5b83\u4e00\u65e6\u627e\u5230\u5339\u914d\u7684\u6570\u636e\u5c31\u4f1a\u505c\u6b62\u67e5\u8be2\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>EXISTS<\/code> \u7528\u4e8e\u68c0\u67e5\u5b50\u67e5\u8be2\u662f\u5426\u8fd4\u56de\u81f3\u5c11\u4e00\u884c\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><code>EXISTS<\/code> \u5b50\u67e5\u8be2\u901a\u5e38\u4e0e <code>SELECT 1<\/code> \u4e00\u8d77\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u53ea\u5173\u5fc3\u5b50\u67e5\u8be2\u662f\u5426\u6709\u7ed3\u679c\uff0c\u800c\u4e0d\u5173\u5fc3\u8fd4\u56de\u7684\u5177\u4f53\u5185\u5bb9\u3002<\/li>\n\n\n\n<li><code>EXISTS<\/code> \u5e38\u7528\u4e8e\u4e0e <code>WHERE<\/code> \u5b50\u53e5\u4e00\u8d77\uff0c\u68c0\u67e5\u67d0\u4e9b\u6761\u4ef6\u662f\u5426\u6ee1\u8db3\u3002<\/li>\n\n\n\n<li><code>EXISTS<\/code> \u548c <code>IN<\/code> \u5728\u529f\u80fd\u4e0a\u6709\u4e9b\u76f8\u4f3c\uff0c\u4f46\u5728\u6027\u80fd\u4e0a\u6709\u4e00\u5b9a\u7684\u5dee\u5f02\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>EXISTS \u662f SQL \u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u5b57\uff0c\u7528\u4e8e\u6d4b\u8bd5\u5b50\u67e5\u8be2\u662f\u5426\u8fd4\u56de\u81f3\u5c11\u4e00\u884c\u7ed3\u679c\u3002EXISTS \u5e38\u7528\u4e8e\u5728\u67e5\u8be2\u4e2d\u68c0\u67e5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-1733","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1733"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1733\/revisions"}],"predecessor-version":[{"id":1734,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1733\/revisions\/1734"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}