{"id":5539,"date":"2024-11-14T09:12:11","date_gmt":"2024-11-14T01:12:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5539\/"},"modified":"2024-11-14T09:12:11","modified_gmt":"2024-11-14T01:12:11","slug":"mysql%e4%ba%a4%e9%9b%86%e6%95%b0%e6%8d%ae%e6%80%8e%e4%b9%88%e8%8e%b7%e5%8f%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5539\/","title":{"rendered":"mysql\u4ea4\u96c6\u6570\u636e\u600e\u4e48\u83b7\u53d6"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\u83b7\u53d6\u4ea4\u96c6\u6570\u636e\u7684\u65b9\u6cd5\u6709\uff1ainner join\uff1a\u5c06\u4e24\u4e2a\u8868\u5173\u8054\uff0c\u4ec5\u8fd4\u56de\u540c\u65f6\u5728\u4e24\u4e2a\u8868\u4e2d\u7684\u884c\u3002exists \u5b50\u67e5\u8be2\uff1a\u68c0\u67e5\u662f\u5426\u5b58\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u3002intersect \u8fd0\u7b97\u7b26\uff1a\u663e\u5f0f\u8ba1\u7b97\u4e24\u4e2a\u8868\u7684\u4ea4\u96c6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/29\/2024052908422599325.jpg\" class=\"aligncenter\" title=\"mysql\u4ea4\u96c6\u6570\u636e\u600e\u4e48\u83b7\u53d6\u63d2\u56fe\" alt=\"mysql\u4ea4\u96c6\u6570\u636e\u600e\u4e48\u83b7\u53d6\u63d2\u56fe\" \/><\/p>\n<h2>\u5982\u4f55\u5728 MySQL \u4e2d\u83b7\u53d6\u4ea4\u96c6\u6570\u636e<\/h2>\n<p><strong>\u4ea4\u96c6\u8fd0\u7b97<\/strong><\/p>\n<p>\u4ea4\u96c6\u8fd0\u7b97\u7528\u4e8e\u83b7\u53d6\u540c\u65f6\u51fa\u73b0\u5728\u4e24\u4e2a\u8868\u4e2d\u7684\u4e00\u7ec4\u884c\u3002\u5bf9\u4e8e\u8868 A \u548c B\uff0c\u5b83\u4eec\u7684\u4ea4\u96c6\u8868\u793a\u4e3a A \u2229 B\uff0c\u5176\u4e2d\u5305\u542b\u540c\u65f6\u51fa\u73b0\u5728 A \u548c B \u4e2d\u7684\u6240\u6709\u884c\u3002<\/p>\n<p><strong>MySQL \u4e2d\u83b7\u53d6\u4ea4\u96c6\u6570\u636e\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u83b7\u53d6\u4ea4\u96c6\u6570\u636e\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 INNER JOIN<\/strong><\/p>\n<p>INNER JOIN \u8bed\u53e5\u7528\u4e8e\u5c06\u4e24\u4e2a\u8868\u8fde\u63a5\u8d77\u6765\uff0c\u5e76\u4ec5\u8fd4\u56de\u51fa\u73b0\u5728\u8fd9\u4e24\u4e2a\u8868\u4e2d\u7684\u884c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT column_list\nFROM table1\nINNER JOIN table2\nON table1.column_name = table2.column_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 EXISTS \u5b50\u67e5\u8be2<\/strong><\/p>\n<p>EXISTS \u5b50\u67e5\u8be2\u7528\u4e8e\u68c0\u67e5\u662f\u5426\u5b58\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT column_list\nFROM table1\nWHERE EXISTS (\n    SELECT 1\n    FROM table2\n    WHERE table1.column_name = table2.column_name\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 INTERSECT \u8fd0\u7b97\u7b26<\/strong><\/p>\n<p>INTERSECT \u8fd0\u7b97\u7b26\u7528\u4e8e\u663e\u5f0f\u8ba1\u7b97\u4e24\u4e2a\u8868\u7684\u4ea4\u96c6\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT column_list\nFROM table1\nINTERSECT\nSELECT column_list\nFROM table2;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868\uff1a<\/p>\n<ul>\n<li> <strong>customers<\/strong> \u8868\uff1a\u5305\u542b\u5ba2\u6237\u4fe1\u606f\uff0c\u5305\u62ec customer_id \u548c customer_name\u3002<\/li>\n<li> <strong>orders<\/strong> \u8868\uff1a\u5305\u542b\u8ba2\u5355\u4fe1\u606f\uff0c\u5305\u62ec order_id \u548c customer_id\u3002<\/li>\n<\/ul>\n<p>\u8981\u83b7\u53d6\u4e0e\u4e24\u4e2a\u8868\u4e2d\u90fd\u6709\u7684\u5ba2\u6237\u76f8\u5bf9\u5e94\u7684\u8ba2\u5355\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT *\nFROM customers\nINNER JOIN orders\nON customers.customer_id = orders.customer_id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6216\u8005\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b EXISTS \u5b50\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT *\nFROM customers\nWHERE EXISTS (\n    SELECT 1\n    FROM orders\n    WHERE customers.customer_id = orders.customer_id\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6216\u8005\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 INTERSECT \u8fd0\u7b97\u7b26\uff1a<\/p>\n<pre>SELECT *\nFROM customers\nINTERSECT\nSELECT *\nFROM orders;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6240\u6709\u4e09\u79cd\u65b9\u6cd5\u90fd\u4f1a\u4ea7\u751f\u76f8\u540c\u7684\u7ed3\u679c\uff1a\u5177\u6709\u540c\u65f6\u51fa\u73b0\u5728 customers \u548c orders \u8868\u4e2d\u7684 customer_id \u7684\u884c\u7684\u96c6\u5408\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4ea4\u96c6\u6570\u636e\u600e\u4e48\u83b7\u53d6\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>\u5728 mysql \u4e2d\u83b7\u53d6\u4ea4\u96c6\u6570\u636e\u7684\u65b9\u6cd5\u6709\uff1ainner join\uff1a\u5c06\u4e24\u4e2a\u8868\u5173\u8054\uff0c\u4ec5\u8fd4\u56de\u540c\u65f6\u5728\u4e24\u4e2a\u8868\u4e2d\u7684\u884c\u3002exists \u5b50\u67e5\u8be2\uff1a\u68c0\u67e5\u662f\u5426\u5b58\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u3002intersect \u8fd0\u7b97\u7b26\uff1a\u663e\u5f0f\u8ba1\u7b97\u4e24\u4e2a\u8868\u7684\u4ea4\u96c6\u3002 \u5982\u4f55\u5728 MySQL \u4e2d\u83b7\u53d6\u4ea4\u96c6\u6570\u636e \u4ea4\u96c6\u8fd0\u7b97 \u4ea4\u96c6\u8fd0\u7b97\u7528\u4e8e\u83b7\u53d6\u540c\u65f6\u51fa\u73b0\u5728\u4e24\u4e2a\u8868\u4e2d\u7684\u4e00\u7ec4\u884c\u3002\u5bf9\u4e8e\u8868 A \u548c B\uff0c\u5b83\u4eec\u7684\u4ea4\u96c6\u8868\u793a\u4e3a A \u2229 B\uff0c\u5176\u4e2d\u5305\u542b\u540c\u65f6\u51fa\u73b0\u5728 A \u548c B \u4e2d\u7684\u6240\u6709\u884c\u3002 MySQL \u4e2d\u83b7\u53d6\u4ea4\u96c6\u6570\u636e\u7684\u65b9\u6cd5 \u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u83b7\u53d6\u4ea4\u96c6\u6570\u636e\uff1a 1. \u4f7f\u7528 INNER JOIN INNER JOIN \u8bed\u53e5\u7528\u4e8e\u5c06\u4e24\u4e2a\u8868\u8fde\u63a5\u8d77\u6765\uff0c\u5e76\u4ec5\u8fd4\u56de\u51fa\u73b0\u5728\u8fd9\u4e24\u4e2a\u8868\u4e2d\u7684\u884c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a SELECT column_list FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name; \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 EXISTS \u5b50\u67e5\u8be2 EXISTS \u5b50\u67e5\u8be2\u7528\u4e8e\u68c0\u67e5\u662f\u5426\u5b58\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a SELECT column_list FROM table1 [&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-5539","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5539","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=5539"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5539\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}