{"id":5423,"date":"2024-11-14T12:30:26","date_gmt":"2024-11-14T04:30:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5423\/"},"modified":"2024-11-14T12:30:26","modified_gmt":"2024-11-14T04:30:26","slug":"mysql%e6%80%8e%e4%b9%88%e5%85%b3%e8%81%94%e4%b8%a4%e4%b8%aa%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5423\/","title":{"rendered":"mysql\u600e\u4e48\u5173\u8054\u4e24\u4e2a\u8868"},"content":{"rendered":"<blockquote><p>\n  \u5173\u8054\u4e24\u4e2a mysql \u8868\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 join \u8bed\u53e5\uff0c\u6307\u5b9a\u516c\u5171\u5217\u8fdb\u884c\u5339\u914d\u3002\u4f7f\u7528 where \u5b50\u53e5\uff0c\u68c0\u67e5\u516c\u5171\u5217\u662f\u5426\u5728\u53e6\u4e00\u8868\u4e2d\u5b58\u5728\u3002\u4f7f\u7528\u5b50\u67e5\u8be2\uff0c\u68c0\u67e5\u53e6\u4e00\u8868\u4e2d\u662f\u5426\u5b58\u5728\u5339\u914d\u8bb0\u5f55\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/28\/2024052812182148422.jpg\" class=\"aligncenter\" title=\"mysql\u600e\u4e48\u5173\u8054\u4e24\u4e2a\u8868\u63d2\u56fe\" alt=\"mysql\u600e\u4e48\u5173\u8054\u4e24\u4e2a\u8868\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5173\u8054\u4e24\u4e2a MySQL \u8868\uff1f<\/strong><\/p>\n<p>\u5173\u8054\u4e24\u4e2a MySQL \u8868\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 JOIN \u8bed\u53e5<\/strong><\/p>\n<pre>SELECT *\nFROM table1\nJOIN table2\nON table1.id = table2.id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>JOIN \u5173\u952e\u5b57\u7528\u4e8e\u8fde\u63a5\u4e24\u4e2a\u8868\u3002<\/li>\n<li>table1 \u548c table2 \u662f\u8981\u5173\u8054\u7684\u4e24\u4e2a\u8868\u3002<\/li>\n<li>ON \u5b50\u53e5\u6307\u5b9a\u8981\u5339\u914d\u7684\u5217\uff0c\u5373\u516c\u5171\u5217\u3002<\/li>\n<\/ul>\n<p><strong>2. \u4f7f\u7528 WHERE \u5b50\u53e5<\/strong><\/p>\n<pre>SELECT *\nFROM table1\nWHERE table1.id IN (SELECT id FROM table2);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>WHERE \u5b50\u53e5\u7528\u4e8e\u8fc7\u6ee4\u6570\u636e\u3002<\/li>\n<li>IN \u5b50\u53e5\u68c0\u67e5 table1.id \u662f\u5426\u5728 table2 \u8868\u7684 id \u5217\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>3. \u4f7f\u7528\u5b50\u67e5\u8be2<\/strong><\/p>\n<pre>SELECT *\nFROM table1\nWHERE EXISTS (SELECT 1 FROM table2 WHERE table2.id = table1.id);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>EXISTS \u5b50\u67e5\u8be2\u68c0\u67e5 table2 \u8868\u4e2d\u662f\u5426\u5b58\u5728\u5339\u914d\u7684\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n<p><strong>\u9009\u62e9\u5173\u8054\u7c7b\u578b<\/strong><\/p>\n<p>\u6709\u56db\u79cd\u4e3b\u8981\u7684\u5173\u8054\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li> <strong>INNER JOIN\uff1a<\/strong>\u4ec5\u8fd4\u56de\u5339\u914d\u884c\u7684\u8bb0\u5f55\u3002<\/li>\n<li> <strong>LEFT JOIN\uff1a<\/strong>\u8fd4\u56de\u6240\u6709\u5de6\u8868\u8bb0\u5f55\uff0c\u5373\u4f7f\u53f3\u8868\u6ca1\u6709\u5339\u914d\u7684\u8bb0\u5f55\u3002<\/li>\n<li> <strong>RIGHT JOIN\uff1a<\/strong>\u8fd4\u56de\u6240\u6709\u53f3\u8868\u8bb0\u5f55\uff0c\u5373\u4f7f\u5de6\u8868\u6ca1\u6709\u5339\u914d\u7684\u8bb0\u5f55\u3002<\/li>\n<li> <strong>FULL OUTER JOIN\uff1a<\/strong>\u8fd4\u56de\u6240\u6709\u5de6\u8868\u548c\u53f3\u8868\u8bb0\u5f55\uff0c\u5373\u4f7f\u6ca1\u6709\u5339\u914d\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u4e24\u4e2a\u8868\u5177\u6709\u516c\u5171\u5217\u3002<\/li>\n<li>\u516c\u5171\u5217\u7684\u7c7b\u578b\u5fc5\u987b\u517c\u5bb9\u3002<\/li>\n<li>\u8003\u8651\u5173\u8054\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u5173\u8054\u4e24\u4e2a\u8868\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>\u5173\u8054\u4e24\u4e2a mysql \u8868\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 join \u8bed\u53e5\uff0c\u6307\u5b9a\u516c\u5171\u5217\u8fdb\u884c\u5339\u914d\u3002\u4f7f\u7528 where \u5b50\u53e5\uff0c\u68c0\u67e5\u516c\u5171\u5217\u662f\u5426\u5728\u53e6\u4e00\u8868\u4e2d\u5b58\u5728\u3002\u4f7f\u7528\u5b50\u67e5\u8be2\uff0c\u68c0\u67e5\u53e6\u4e00\u8868\u4e2d\u662f\u5426\u5b58\u5728\u5339\u914d\u8bb0\u5f55\u3002 \u5982\u4f55\u5173\u8054\u4e24\u4e2a MySQL \u8868\uff1f \u5173\u8054\u4e24\u4e2a MySQL \u8868\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a 1. \u4f7f\u7528 JOIN \u8bed\u53e5 SELECT * FROM table1 JOIN table2 ON table1.id = table2.id; \u767b\u5f55\u540e\u590d\u5236 JOIN \u5173\u952e\u5b57\u7528\u4e8e\u8fde\u63a5\u4e24\u4e2a\u8868\u3002 table1 \u548c table2 \u662f\u8981\u5173\u8054\u7684\u4e24\u4e2a\u8868\u3002 ON \u5b50\u53e5\u6307\u5b9a\u8981\u5339\u914d\u7684\u5217\uff0c\u5373\u516c\u5171\u5217\u3002 2. \u4f7f\u7528 WHERE \u5b50\u53e5 SELECT * FROM table1 WHERE table1.id IN (SELECT id FROM table2); \u767b\u5f55\u540e\u590d\u5236 WHERE \u5b50\u53e5\u7528\u4e8e\u8fc7\u6ee4\u6570\u636e\u3002 IN [&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-5423","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5423","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=5423"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5423\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}