{"id":5994,"date":"2024-11-14T09:20:06","date_gmt":"2024-11-14T01:20:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5994\/"},"modified":"2024-11-14T09:20:06","modified_gmt":"2024-11-14T01:20:06","slug":"mysql-union%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5994\/","title":{"rendered":"mysql union\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  mysql \u7684 union \u64cd\u4f5c\u7b26\u7528\u4e8e\u7ec4\u5408\u4e0d\u540c\u8868\u6216\u76f8\u540c\u8868\u4e2d\u591a\u4e2a\u7ed3\u679c\u96c6\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u552f\u4e00\u884c\u7684\u7ec4\u5408\u7ed3\u679c\u3002\u5b83\u9700\u8981\u786e\u4fdd\u5217\u6570\u548c\u6570\u636e\u7c7b\u578b\u76f8\u540c\uff0c\u4f7f\u7528 union all \u65f6\u91cd\u590d\u884c\u5c06\u88ab\u4fdd\u7559\uff0c\u5426\u5219\u5c06\u88ab\u79fb\u9664\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/12\/2024061213482851576.jpg\" class=\"aligncenter\" title=\"mysql union\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"mysql union\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL UNION \u64cd\u4f5c<\/strong><\/p>\n<p>UNION \u64cd\u4f5c\u7b26\u7528\u4e8e\u7ec4\u5408\u6765\u81ea\u4e0d\u540c\u8868\u6216\u540c\u4e00\u8868\u7684\u4e24\u4e2a\u6216\u66f4\u591a\u7ed3\u679c\u96c6\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u552f\u4e00\u884c\u7684\u7ec4\u5408\u7ed3\u679c\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT \u5217\u540d1, \u5217\u540d2, ...\nFROM \u88681\nUNION\nSELECT \u5217\u540d1, \u5217\u540d2, ...\nFROM \u88682\n[UNION ALL\nSELECT \u5217\u540d1, \u5217\u540d2, ...\nFROM \u88683];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 UNION \u64cd\u4f5c\u7b26\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u786e\u4fdd\u5217\u6570\u548c\u6570\u636e\u7c7b\u578b\u5339\u914d\uff1a<\/strong>\u53c2\u4e0e UNION \u64cd\u4f5c\u7684\u8868\u7684\u5217\u6570\u548c\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u76f8\u540c\u3002<\/li>\n<li> <strong>\u4f7f\u7528 UNION ALL \u79fb\u9664\u91cd\u590d\u9879\uff1a<\/strong>\u5982\u679c\u9700\u8981\u4fdd\u7559\u91cd\u590d\u884c\uff0c\u8bf7\u4f7f\u7528 UNION ALL\u3002\u5426\u5219\uff0c\u8bf7\u4f7f\u7528 UNION\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709 &#8220;customers&#8221; \u548c &#8220;orders&#8221; \u4e24\u4e2a\u8868\uff1a<\/p>\n<pre>+---------+-------+\n| customers | name  |\n+---------+-------+\n| 1        | John   |\n| 2        | Mary   |\n| 3        | Bob    |\n+---------+-------+\n\n+--------+---------+---------+\n| orders  | customer_id | product |\n+--------+---------+---------+\n| 1       | 1         | Shoes   |\n| 2       | 2         | Shirt   |\n| 3       | 1         | Hat     |\n+--------+---------+---------+<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0b\u67e5\u8be2\u4f7f\u7528 UNION \u64cd\u4f5c\u7b26\u8fd4\u56de\u5ba2\u6237\u59d3\u540d\u4ee5\u53ca\u4ed6\u4eec\u8d2d\u4e70\u7684\u4ea7\u54c1\uff1a<\/p>\n<pre>SELECT name, product\nFROM customers\nUNION\nSELECT name, product\nFROM orders;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre>+-------+---------+\n| name  | product |\n+-------+---------+\n| John   | Shoes   |\n| Mary   | Shirt   |\n| Bob    | Hat     |\n+-------+---------+<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f union\u600e\u4e48\u7528\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>mysql \u7684 union \u64cd\u4f5c\u7b26\u7528\u4e8e\u7ec4\u5408\u4e0d\u540c\u8868\u6216\u76f8\u540c\u8868\u4e2d\u591a\u4e2a\u7ed3\u679c\u96c6\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u552f\u4e00\u884c\u7684\u7ec4\u5408\u7ed3\u679c\u3002\u5b83\u9700\u8981\u786e\u4fdd\u5217\u6570\u548c\u6570\u636e\u7c7b\u578b\u76f8\u540c\uff0c\u4f7f\u7528 union all \u65f6\u91cd\u590d\u884c\u5c06\u88ab\u4fdd\u7559\uff0c\u5426\u5219\u5c06\u88ab\u79fb\u9664\u3002 MySQL UNION \u64cd\u4f5c UNION \u64cd\u4f5c\u7b26\u7528\u4e8e\u7ec4\u5408\u6765\u81ea\u4e0d\u540c\u8868\u6216\u540c\u4e00\u8868\u7684\u4e24\u4e2a\u6216\u66f4\u591a\u7ed3\u679c\u96c6\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u552f\u4e00\u884c\u7684\u7ec4\u5408\u7ed3\u679c\u3002 \u8bed\u6cd5\uff1a SELECT \u5217\u540d1, \u5217\u540d2, &#8230; FROM \u88681 UNION SELECT \u5217\u540d1, \u5217\u540d2, &#8230; FROM \u88682 [UNION ALL SELECT \u5217\u540d1, \u5217\u540d2, &#8230; FROM \u88683]; \u767b\u5f55\u540e\u590d\u5236 \u5982\u4f55\u4f7f\u7528 UNION \u64cd\u4f5c\u7b26\uff1a \u786e\u4fdd\u5217\u6570\u548c\u6570\u636e\u7c7b\u578b\u5339\u914d\uff1a\u53c2\u4e0e UNION \u64cd\u4f5c\u7684\u8868\u7684\u5217\u6570\u548c\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u76f8\u540c\u3002 \u4f7f\u7528 UNION ALL \u79fb\u9664\u91cd\u590d\u9879\uff1a\u5982\u679c\u9700\u8981\u4fdd\u7559\u91cd\u590d\u884c\uff0c\u8bf7\u4f7f\u7528 UNION ALL\u3002\u5426\u5219\uff0c\u8bf7\u4f7f\u7528 UNION\u3002 \u793a\u4f8b\uff1a \u5047\u8bbe\u6211\u4eec\u6709 &#8220;customers&#8221; \u548c &#8220;orders&#8221; \u4e24\u4e2a\u8868\uff1a +&#8212;&#8212;&#8212;+&#8212;&#8212;-+ [&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-5994","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5994","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=5994"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5994\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}