{"id":2407,"date":"2023-10-12T17:21:23","date_gmt":"2023-10-12T09:21:23","guid":{"rendered":"https:\/\/white-night.club\/?p=2407"},"modified":"2023-11-09T11:07:50","modified_gmt":"2023-11-09T03:07:50","slug":"db5","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/10\/12\/db5\/","title":{"rendered":"Mysql:\u901a\u8fc7NOT EXISTS\u9009\u62e9\u53c2\u4e0e\u5168\u90e8\u4e8b\u4ef6\u7684\u6570\u636e"},"content":{"rendered":"\n<p>\u603b\u7ed3\u4e0bNOT EXISTS\u5d4c\u5957\u4f7f\u7528\u7684\u8be6\u89e3\u548c\u5fc3\u5f97\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u4ec0\u4e48\u662fNOT EXISTS<\/p>\n<cite><em>\u7b80\u5355\u4ecb\u7ecd<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u5148\u6765\u8bf4\u8bf4EXISTS\uff0c\u5982\u679c\u5b58\u5728\u4e00\u6761\u6216\u591a\u6761\u8bb0\u5f55\u5c31\u8fd4\u56detrue\uff0c\u4e0d\u7136\u8fd4\u56defalse\u3002\u5373EXISTS\u53ea\u8fd4\u56de\u771f\u503c\u6216\u5047\u503c\uff0c\u5728SQL\u4e2d\u4f5c\u4e3a\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u4f7f\u7528\u3002\u7b80\u5355\u6765\u8bf4EXISTS\u5c31\u50cfC\u8bed\u8a00\u4e2d\u7684\u5faa\u73af\u8bed\u53e5\u3002\u5177\u4f53\u7528\u4f8b\u53ef\u4ee5\u53c2\u8003\u8fd9\u7bc7\u6587\u7ae0<a href=\"https:\/\/white-night.club\/index.php\/2023\/10\/12\/ad41\/\" data-type=\"URL\" data-id=\"https:\/\/white-night.club\/index.php\/2023\/10\/12\/ad41\/\"><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0086ff\" class=\"has-inline-color\">181. \u8d85\u8fc7\u7ecf\u7406\u6536\u5165\u7684\u5458\u5de5<\/mark><\/a>\u3002\u8fd9\u91cc\u53ea\u8bb2\u4e0bSQL\u8bed\u53e5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT A.name AS Employee\nFROM Employee AS A\nWHERE EXISTS(\n  SELECT B.*FROM Employee AS B\n  WHERE A.managerId=B.id AND A.salary&gt;B.salary\n)<\/code><\/pre>\n\n\n\n<p>\u5185\u5c42\u7684EXISTS\u5c31\u662f\u4e00\u4e2afor\u5faa\u73af\uff0c\u800c\u6574\u4e2aSQL\u8bed\u53e5\u5c31\u50cf\u4e00\u4e2a\u53cc\u91cdfor\u5faa\u73af\u8bed\u53e5\u3002\u53ea\u8981EXISTS\u4e2d\u6709\u4e00\u884c\u7684\u6570\u636e\u6ee1\u8db3WHERE\u6761\u4ef6\uff0c\u90a3\u5185\u5c42\u7684\u5faa\u73af\u5c31\u4f1a\u8fd4\u56detrue\u3002\u800c\u5916\u5c42\u7684SELECT\u5c31\u77e5\u9053\uff1a\u201c<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u54e6\uff0c\u8fd9\u4e00\u884cname\u6ee1\u8db3\u6211\u7684\u6761\u4ef6\uff0c\u6240\u4ee5\u6211\u8981\u4ece\u6570\u636e\u5e93\u4e2d\u9009\u53d6\u8be5\u884c\u7684\u6570\u636e<\/mark>\u201d\u3002<\/p>\n\n\n\n<p>\u800cNOT EXISTS\u90a3\u5c31\u662f\u987e\u540d\u601d\u4e49\uff0c\u5982\u679c\u628aEXISTS\u6539\u6210NOT EXISTS\uff0c\u6b64\u65f6\u82e5\u67d0\u4e00\u884c\u7684\u6570\u636e\u6ee1\u8db3\u5185\u5c42\u7684WHERE\u6761\u4ef6\uff0c\u90a3\u4e48WHERE NOT EXISTS\u5c31\u4f1a\u8fd4\u56detrue\u3002\u6b64\u65f6\u5bf9\u4e8e\u5916\u5c42\u7684SELECT\u6765\u8bf4\u5c31\u662f\uff1a\u201c<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u54e6\uff0c\u8fd9\u4e00\u884c\u6ee1\u8db3\u5185\u5c42\u7684WHERE\u6761\u4ef6\uff0c<strong>\u800c\u6211\u8981\u627e\u7684\u662f\u4e0d\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u6570\u636e<\/strong>\uff0c\u6240\u4ee5\u4e0d\u4ece\u6570\u636e\u5e93\u4e2d\u9009\u53d6\u8be5\u6570\u636e<\/mark>\u201d\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>NOT EXISTS\u5b9e\u4f8b<\/p>\n<cite><em>Leetcode\u9898\u76ee<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u4ee5Leetcode\u9898\u76ee\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/customers-who-bought-all-products\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0086ff\" class=\"has-inline-color\">1045.&nbsp;\u4e70\u4e0b\u6240\u6709\u4ea7\u54c1\u7684\u5ba2\u6237<\/mark><\/a>\u4e3a\u4f8b\u3002<\/p>\n\n\n\n<p>\u9898\u76ee\u8981\u6c42\u627e\u51fa\u201c\u4e70\u4e0b\u6240\u6709\u4ea7\u54c1\u201d\u7684\u5ba2\u6237\uff0c\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u627e\u51fa\u6ee1\u8db3\u201c\u4e70\u4e0b\u6240\u6709\u4ea7\u54c1\u201d\u6761\u4ef6\u7684\u6570\u636e\u884c\u3002<\/p>\n\n\n\n<p>\u7528EXISTS\u53bb\u5199\uff0c\u5b9e\u5728\u662f\u4e0d\u597d\u60f3\u3002\u76f4\u63a5\u9009\u53d6\u4e70\u4e86\u5168\u90e8\u4ea7\u54c1\u7684\u5ba2\u6237\uff0c\u90a3\u8be5\u5982\u4f55\u904d\u5386Customer\u8868\u5462\uff1fWHERE\u6761\u4ef6\u8be5\u600e\u4e48\u5904\u7406\u5462\uff1f\u4e0d\u5982\u53cd\u8fc7\u6765\u60f3\uff1a<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u9009\u53d6\u6ca1\u6709\u8d2d\u4e70\u5168\u90e8\u4ea7\u54c1\u7684\u5ba2\u6237<\/mark>\u3002\u5982\u679c\u5728product\u8868\u548ccustomer\u8868\u4e2d\uff0c\u627e\u4e0d\u9f50\u67d0\u4e2a\u7528\u6237\u8d2d\u4e70\u7684\u4ea7\u54c1id\uff08\u6765\u81eaproduct\u8868\uff09\uff0c\u5c31\u8bf4\u660e\u8fd9\u4e2a\u5ba2\u6237\u6ca1\u6709\u8d2d\u4e70\u5168\u90e8\u4ea7\u54c1\u3002\u90a3\u6700\u540e\u52a0\u4e2aNOT\u4e0d\u5c31\u80fd\u9009\u53d6\u4e70\u4e86\u5168\u90e8\u4ea7\u54c1\u7684\u5ba2\u6237\u5417\uff1f<\/p>\n\n\n\n<p>\u90a3\u5148\u6765\u60f3\u4e0b\u600e\u4e48\u9009\u53d6\u201c\u6ca1\u6709\u8d2d\u4e70\u5168\u90e8\u4ea7\u54c1\u7684\u5ba2\u6237\u201d\u3002\u7531\u4e8eCustom\u8868\u6709\u91cd\u590d\u503c\uff0c\u90a3\u4e48\u5f97\u5148\u53bb\u91cd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT A.customer_id FROM (SELECT DISTINCT customer_id FROM Customer) AS A<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote has-vivid-cyan-blue-color has-text-color\">\n<p>\u5199\u8005\u6ce8<\/p>\n<cite>\u5982\u679c\u5199\u6210SELECT DISTINCT A.customer_id FROM CUstomer AS A\uff0c\u5728Leetcode\u8fd0\u884c\u4f1a\u8d85\u65f6\u3002\u8fd9\u70b9\u53ef\u80fd\u6d89\u53ca\u5230\u6570\u636e\u5e93\u7684\u4f18\u5316\u4e4b\u7c7b\u7684\uff08SQL\u4f18\u5316\uff1a\u8c13\u8bcd\u4e0b\u63a8\uff09\u3002\u6211\u7684\u4e2a\u4eba\u7406\u89e3\u662f\u524d\u8005\u5148\u4eceCustomer\u8868\u4e2d\u53bb\u91cd\uff0c\u4eceCustomer\u8868\u4e2d\u9009\u53d6\u4e0d\u91cd\u590d\u7684\u6570\u636e\u751f\u6210\u66f4\u5c0f\u7684\u865a\u62df\u8868virtualCustomer\u8868\u3002\u5bf9\u8fd9\u4e2a\u66f4\u5c0f\u7684\u8868\u8fdb\u884cWHERE\u6761\u4ef6\u7684\u5224\u65ad\u5e76\u5c06\u7ed3\u679c\u4f5c\u4e3a\u65b0\u7684\u865a\u62df\u8868virtualCustomer2\uff0c\u6700\u540eSELECT\u662f\u4ecevitrualCustomer2\u4e2d\u9009\u53d6\u7684\u3002<br>\u4f46\u662f\u540e\u8005\u7684DISTINCT\u662f\u653eSELECT\u4e2d\u7684\u3002SELECT\u867d\u7136\u5199\u5728SQL\u8bed\u53e5\u7684\u5f00\u5934\uff0c\u4f46\u5b9e\u9645\u4e0a\u5b83\u5728\u6570\u636e\u5e93\u4e2d\u7684\u6267\u884c\u987a\u5e8f\u662f\u5728\u6700\u540e\u3002\u76f8\u5f53\u4e8e\u6211\u5148\u4eceCustomer\u8868\u4e2d\u9009\u53d6\u6240\u6709\u6570\u636e\u751f\u6210\u865a\u62df\u8868vitrualCustomer\u3002\u63a5\u4e0b\u6765\u7684WHERE\u6761\u4ef6\u5224\u65ad\u4f1a\u5bf9\u8868\u4e2d\u6240\u6709id\u90fd\u6267\u884c\u4e00\u904d\uff0c\u6267\u884c\u7684\u7ed3\u679c\u751f\u6210\u53e6\u4e00\u4e2a\u865a\u62df\u8868vitrualCustomer2\uff0c\u6700\u540e\u518d\u5bf9\u8fd9\u4e2a\u865a\u62df\u8868\u53bb\u91cdSELECT\u5f97\u5230\u6211\u4eec\u8981\u7684\u6570\u636e\u3002<br>\u524d\u8005\u662f\u5148\u7b5b\u6389\u91cd\u590d\u6570\u636e\uff0c\u518d\u5bf9\u5269\u4e0b\u7684\u6570\u636e\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u3002\u540e\u8005\u662f\u5148\u6761\u4ef6\u5224\u65ad\uff0c\u518d\u7b5b\u6389\u91cd\u590d\u7684customer_id\u3002\u660e\u663e\u540e\u8005\u5728\u6761\u4ef6\u5224\u65ad\u7684\u65f6\u5019\u8fd8\u4f1a\u5bf9\u591a\u4f59\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406\uff0c\u6548\u7387\u6bd4\u524d\u8005\u4f4e\u3002<br>\u66f4\u5177\u4f53\u7684\u5185\u5bb9\u4f1a\u5728\u672c\u6587\u7684\u540e\u534a\u90e8\u5206\u63d0\u5230\u3002\u6211\u4e4b\u524d\u6ca1\u63a5\u89e6\u8fc7SQL\u4f18\u5316\uff0c\u8fd9\u91cc\u662f\u78b0\u5230\u8fd9\u4e2a\u95ee\u9898\u53d1\u73b0\u81ea\u5df1\u5b9e\u5728\u6ca1\u6cd5\u89e3\u51b3\uff0c\u6309\u7740\u201c\u53ef\u80fd\u548cSQL\u4f18\u5316\u6709\u5173\u201d\u7684\u601d\u8def\u5c31\u53bb\u7f51\u4e0a\u627e\u4e86\u4e0b\uff0c\u6700\u540e\u627e\u5230\u4e86\u8c13\u8bcd\u4e0b\u63a8\u8fd9\u4e2aSQL\u4f18\u5316\u7684\u65b9\u6cd5\u3002<\/cite><\/blockquote>\n\n\n\n<p>id\u53bb\u91cd\u4e4b\u540e\uff0c\u65e2\u7136\u8981\u9009\u201c\u6ca1\u6709\u8d2d\u4e70\u5168\u90e8\u4ea7\u54c1\u201d\u7684\u5ba2\u6237\uff0c\u6700\u540e\u518d\u52a0NOT\u5f97\u5230\u201c\u4e70\u4e86\u5168\u90e8\u4ea7\u54c1\u5ba2\u6237\u201d\u7684\u96c6\u5408\u3002\u5f88\u660e\u663e\u6700\u5916\u5c42\u8981\u4e00\u4e2aNOT EXISTS\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Write your MySQL query statement below\nSELECT A.customer_id FROM (SELECT DISTINCT customer_id FROM Customer) AS A\nWHERE NOT EXISTS(\n    \n)<\/code><\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\u60f3\u4e0b\u600e\u4e48\u9009\u62e9\u201c\u6ca1\u6709\u8d2d\u4e70\u5168\u90e8\u4ea7\u54c1\u201d\u7684\u5ba2\u6237\u3002\u5f88\u660e\u663e\u8981\u4eceProduct\u8868\u4e2d\u5148\u9009\u4ea7\u54c1id\uff0c\u518d\u5728Customer\u8868\u4e2d\u67e5\u8be2\u3002\u56e0\u4e3a\u53ea\u8981\u6ca1\u6709\u8d2d\u4e70\u5168\u90e8\u4ea7\u54c1\uff0c\u90a3\u4e48\u5728Customer\u8868\u4e2d\u67e5\u8be2\u6ca1\u88ab\u8d2d\u4e70\u7684\u4ea7\u54c1id\u65f6\uff0c\u67e5\u8be2\u7ed3\u679c\u4f1a\u8fd4\u56de\u7a7a\u3002<\/p>\n\n\n\n<p>\u8fd9\u610f\u5473\u7740\u6211\u4eec\u4e0d\u80fd\u7528EXISTS\u3002\u56e0\u4e3a\u5982\u679c\u7528EXISTS\uff0c\u90a3\u4e48<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u53ea\u8981\u8be5\u5ba2\u6237\u4e70\u4e86\u4efb\u610f\u4ea7\u54c1\uff0cEXISTS\u5c31\u4f1a\u8fd4\u56de\u771f\u503c<\/mark>\u3002\u6700\u540e\u5c31\u53d8\u6210\u4e86\u201c\u67e5\u627e<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u4e0d\u5b58\u5728<\/mark>\uff08\u7b2c\u4e00\u6b21NOT EXISTS\uff09<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u6709<\/mark>\u8d2d\u4e70\u8fc7\u4efb\u610f\u4e00\u4ef6\u4ea7\u54c1\uff08\u7b2c\u4e8c\u6b21EXISTS\uff09\u7684\u5ba2\u6237\u201d\u3002\u800c\u6211\u4eec\u8981\u7684\u662f\u201c\u67e5\u627e<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u4e0d\u5b58\u5728\uff08<\/mark>\u7b2c\u4e00\u6b21NOT EXISTS\uff09<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u6ca1\u6709<\/mark>\u8d2d\u4e70\u8fc7\u4efb\u4f55\u4e00\u4ef6\u4ea7\u54c1\uff08\u7b2c\u4e8c\u6b21NOT EXISTS\uff09\u7684\u5ba2\u6237&#8221;.<\/p>\n\n\n\n<p>\u786e\u5b9a\u8981\u7528NOT EXISTS\u4e4b\u540e\uff0c\u90a3\u5c31\u662f\u5199\u4e24\u4e2a\u5224\u65ad\u6761\u4ef6\u53bb\u5339\u914d\uff0c\u4e0d\u96be\u7406\u89e3\u5c31\u4e0d\u4f5c\u89e3\u91ca\u4e86\u3002<\/p>\n\n\n\n<p>\u7531\u4e8eEXISTS\u6700\u540e\u8fd4\u56de\u7684\u90fd\u662f\u5e03\u5c14\u503c\uff0c\u6240\u4ee5SELECT \u4ec0\u4e48\u90fd\u662f\u6ca1\u95ee\u9898\u7684\uff0cSELECT 1\u4e5f\u662fOK\u7684\u3002\u6700\u540e\u5927\u81f4\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Write your MySQL query statement below\nSELECT A.customer_id FROM (SELECT DISTINCT customer_id FROM Customer) AS A\nWHERE NOT EXISTS(\n    SELECT 1 FROM Product AS B\n    WHERE NOT EXISTS(\n        SELECT 1 FROM Customer AS C\n        WHERE A.customer_id=C.customer_id AND B.Product_key=C.Product_key\n    )    \n)<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote has-vivid-cyan-blue-color has-text-color\">\n<p>\u5199\u8005\u6ce8<\/p>\n<cite>\u56de\u5230\u6211\u4eec\u4e00\u5f00\u59cb\u63d0\u5230\u7684SQL\u4f18\u5316\u95ee\u9898\u3002\u5047\u8bbe\u8868\u4e2d\u6709100\u4e2aid\uff0c\u5176\u4e2d80\u6761\u662f\u91cd\u590d\u7684\u3002\u5728 A.customer_id=C.customer_id\u4e2d\uff0c\u5982\u679c\u4f60\u628a\u53bb\u91cd\u7559\u5230\u6700\u540e\uff0c\u90a3\u4e48\u8fd9\u91cc\u7684A.customer_id\u4f1a\u5305\u542b\u8fd980\u6761\u91cd\u590d\u7684id\u3002\u6b64\u65f6\u53ea\u6709\u5f53\u5224\u65ad\u6761\u4ef6WHERE\u5168\u90e8\u7ed3\u675f\u540e\u624d\u4f1a\u53bb\u6389\u91cd\u590d\u7684id\u3002\u800c\u5148\u53bb\u91cd\uff08\u8c13\u8bcd\u4e0b\u63a8\uff09\u518d\u8fdb\u884c\u5224\u65ad\uff0c \u90a3\u4e48A.customer_id=C.customer_id\u53ea\u9700\u8981\u4ece\u8fd920\u6761id\u4e2d\u4e00\u6761\u4e00\u6761\u5224\u65ad\uff0c\u6267\u884c\u6b21\u6570\u8981\u6bd4\u524d\u8005\u5c11\u5f88\u591a\u3002<\/cite><\/blockquote>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>NOT EXISTS\u5d4c\u5957\u9009\u62e9<\/p>\n","protected":false},"author":1,"featured_media":3123,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[69,35],"tags":[99],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/2407"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=2407"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/2407\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/3123"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=2407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=2407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=2407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}