{"id":7465,"date":"2024-11-14T09:41:23","date_gmt":"2024-11-14T01:41:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7465\/"},"modified":"2024-11-14T09:41:23","modified_gmt":"2024-11-14T01:41:23","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8%e7%b4%a2%e5%bc%95%e8%a1%a8%e5%ae%9e%e7%8e%b0-mysql-%e4%b8%ad%e7%9a%84%e5%bf%ab%e9%80%9f%e6%a8%a1%e7%b3%8a%e6%90%9c%e7%b4%a2%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7465\/","title":{"rendered":"\u5982\u4f55\u5229\u7528\u7d22\u5f15\u8868\u5b9e\u73b0 MySQL \u4e2d\u7684\u5feb\u901f\u6a21\u7cca\u641c\u7d22\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173087802421661.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528\u7d22\u5f15\u8868\u5b9e\u73b0 MySQL \u4e2d\u7684\u5feb\u901f\u6a21\u7cca\u641c\u7d22\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528\u7d22\u5f15\u8868\u5b9e\u73b0 MySQL \u4e2d\u7684\u5feb\u901f\u6a21\u7cca\u641c\u7d22\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5229\u7528\u7d22\u5f15\u8868\u8fdb\u884c\u5feb\u901f\u6a21\u7cca\u641c\u7d22<\/strong><\/p>\n<p>\u5f53\u6570\u636e\u91cf\u5e9e\u5927\u65f6\uff0c\u5728  \u4e2d\u5b9e\u73b0\u9ad8\u6548\u6a21\u7cca\u67e5\u8be2\u662f\u4e00\u9879\u6311\u6218\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u4e00\u79cd\u901a\u8fc7\u7d22\u5f15\u8868\u4f18\u5316\u6a21\u7cca\u641c\u7d22\u6027\u80fd\u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4e00\u79d2\u5185\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<p><strong>\u7d22\u5f15\u8868\u7684\u8bbe\u8ba1<\/strong><\/p>\n<p>\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7d22\u5f15\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4e09\u5217\uff1a<\/p>\n<ul>\n<li>\u5f53\u524d\u8bcd<\/li>\n<li>\u4e0b\u4e00\u8bcd<\/li>\n<li>\u539f\u8bb0\u5f55\u4e3b\u952e id<\/li>\n<\/ul>\n<p>\u6211\u4eec\u5bf9\u6570\u636e\u4e2d\u7684\u6bcf\u4e2a\u8bb0\u5f55\u521b\u5efa\u4e00\u884c\uff0c\u5e76\u4ee5\u76f8\u53cd\u7684\u5355\u8bcd\u987a\u5e8f\u5b58\u50a8\u5355\u8bcd\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u8bb0\u5f55\u201c\u6a21\u7cca\u641c\u7d22\u201d\uff0c\u6211\u4eec\u521b\u5efa\u5982\u4e0b\u884c\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u5f53\u524d\u8bcd<\/th>\n<th>\u4e0b\u4e00\u8bcd<\/th>\n<th>\u539f\u8bb0\u5f55\u4e3b\u952e id<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u7d22<\/td>\n<td>null<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>\u641c<\/td>\n<td>\u7d22<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>\u7cca<\/td>\n<td>\u641c<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>\u6a21<\/td>\n<td>\u7cca<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u6a21\u7cca\u641c\u7d22\u67e5\u8be2<\/strong><\/p>\n<p>\u8981\u6a21\u7cca\u641c\u7d22\u201c\u6a21\u7cca\u641c\u7d22\u201d\uff0c\u6211\u4eec\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT \u539f\u8bb0\u5f55\u4e3b\u952eID\nFROM (SELECT \u539f\u8bb0\u5f55\u4e3b\u952eID FROM \u7d22\u5f15\u8868 WHERE \u5f53\u524d\u8bcd = '\u6a21' AND \u4e0b\u4e00\u8bcd = '\u7cca')\nJOIN (SELECT \u539f\u8bb0\u5f55\u4e3b\u952eID FROM \u7d22\u5f15\u8868 WHERE \u5f53\u524d\u8bcd = '\u7cca' AND \u4e0b\u4e00\u8bcd = '\u641c')\nJOIN (SELECT \u539f\u8bb0\u5f55\u4e3b\u952eID FROM \u7d22\u5f15\u8868 WHERE \u5f53\u524d\u8bcd = '\u641c' AND \u4e0b\u4e00\u8bcd = '\u7d22')\nJOIN (SELECT \u539f\u8bb0\u5f55\u4e3b\u952eID FROM \u7d22\u5f15\u8868 WHERE \u5f53\u524d\u8bcd = '\u7d22' AND \u4e0b\u4e00\u8bcd IS NULL)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e2a\u67e5\u8be2\u901a\u8fc7\u8fde\u63a5\u7d22\u5f15\u8868\u4e2d\u76f8\u90bb\u5355\u8bcd\u7684\u8bb0\u5f55\uff0c\u4ee5\u9010\u4e2a\u5355\u8bcd\u5339\u914d\u7684\u65b9\u5f0f\uff0c\u9ad8\u6548\u5730\u68c0\u7d22\u5339\u914d\u539f\u8bb0\u5f55\u7684\u4e3b\u952e id\u3002<\/p>\n<p><strong>\u6027\u80fd\u4f18\u52bf<\/strong><\/p>\n<p>\u8fd9\u79cd\u57fa\u4e8e\u7d22\u5f15\u8868\u7684\u65b9\u6cd5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u63d0\u9ad8\u4e86\u6a21\u7cca\u641c\u7d22\u6027\u80fd\uff1a<\/p>\n<ul>\n<li>\u5b83\u907f\u514d\u4e86\u5168\u8868\u626b\u63cf\uff0c\u4ece\u800c\u663e\u8457\u51cf\u5c11\u4e86\u67e5\u8be2\u65f6\u95f4\u3002<\/li>\n<li>\u5b83\u5229\u7528\u4e86\u7d22\u5f15\uff0c\u4f7f\u67e5\u8be2\u4ee5 o(n) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u6267\u884c\u3002<\/li>\n<li>\u5b83\u53ef\u4ee5\u8f7b\u677e\u5730\u6269\u5c55\u5230\u66f4\u5927\u7684\u6570\u636e\u96c6\uff0c\u4fdd\u6301\u9ad8\u901f\u641c\u7d22\u3002<\/li>\n<\/ul>\n<p><strong>\u5c40\u9650\u6027<\/strong><\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u6b64\u65b9\u6cd5\u5904\u7406\u7cbe\u786e\u641c\u7d22\u800c\u4e0d\u662f\u6a21\u7cca\u5339\u914d\u3002\u6b64\u5916\uff0c\u4e3a\u4e86\u7ef4\u62a4\u7d22\u5f15\u8868\uff0c\u9700\u8981\u5b9a\u671f\u66f4\u65b0\u4ee5\u53cd\u6620\u6570\u636e\u66f4\u6539\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528\u7d22\u5f15\u8868\u5b9e\u73b0 MySQL \u4e2d\u7684\u5feb\u901f\u6a21\u7cca\u641c\u7d22\uff1f\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>\u5229\u7528\u7d22\u5f15\u8868\u8fdb\u884c\u5feb\u901f\u6a21\u7cca\u641c\u7d22 \u5f53\u6570\u636e\u91cf\u5e9e\u5927\u65f6\uff0c\u5728 \u4e2d\u5b9e\u73b0\u9ad8\u6548\u6a21\u7cca\u67e5\u8be2\u662f\u4e00\u9879\u6311\u6218\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u4e00\u79cd\u901a\u8fc7\u7d22\u5f15\u8868\u4f18\u5316\u6a21\u7cca\u641c\u7d22\u6027\u80fd\u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4e00\u79d2\u5185\u8fd4\u56de\u7ed3\u679c\u3002 \u7d22\u5f15\u8868\u7684\u8bbe\u8ba1 \u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7d22\u5f15\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4e09\u5217\uff1a \u5f53\u524d\u8bcd \u4e0b\u4e00\u8bcd \u539f\u8bb0\u5f55\u4e3b\u952e id \u6211\u4eec\u5bf9\u6570\u636e\u4e2d\u7684\u6bcf\u4e2a\u8bb0\u5f55\u521b\u5efa\u4e00\u884c\uff0c\u5e76\u4ee5\u76f8\u53cd\u7684\u5355\u8bcd\u987a\u5e8f\u5b58\u50a8\u5355\u8bcd\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u8bb0\u5f55\u201c\u6a21\u7cca\u641c\u7d22\u201d\uff0c\u6211\u4eec\u521b\u5efa\u5982\u4e0b\u884c\uff1a \u5f53\u524d\u8bcd \u4e0b\u4e00\u8bcd \u539f\u8bb0\u5f55\u4e3b\u952e id \u7d22 null 1 \u641c \u7d22 1 \u7cca \u641c 1 \u6a21 \u7cca 1 \u6a21\u7cca\u641c\u7d22\u67e5\u8be2 \u8981\u6a21\u7cca\u641c\u7d22\u201c\u6a21\u7cca\u641c\u7d22\u201d\uff0c\u6211\u4eec\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a SELECT \u539f\u8bb0\u5f55\u4e3b\u952eID FROM (SELECT \u539f\u8bb0\u5f55\u4e3b\u952eID FROM \u7d22\u5f15\u8868 WHERE \u5f53\u524d\u8bcd = &#8216;\u6a21&#8217; AND \u4e0b\u4e00\u8bcd = &#8216;\u7cca&#8217;) JOIN (SELECT \u539f\u8bb0\u5f55\u4e3b\u952eID FROM \u7d22\u5f15\u8868 WHERE \u5f53\u524d\u8bcd = &#8216;\u7cca&#8217; AND \u4e0b\u4e00\u8bcd = [&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-7465","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7465","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=7465"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7465\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}