{"id":5275,"date":"2024-11-14T11:06:24","date_gmt":"2024-11-14T03:06:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5275\/"},"modified":"2024-11-14T11:06:24","modified_gmt":"2024-11-14T03:06:24","slug":"mysql%e4%b8%adunionall%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5275\/","title":{"rendered":"mysql\u4e2dunionall\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  mysql \u4e2d\u7684 union all \u8fd0\u7b97\u7b26\u7ec4\u5408\u591a\u4e2a select \u8bed\u53e5\u7684\u7ed3\u679c\uff0c\u800c\u4e0d\u53bb\u9664\u91cd\u590d\u884c\uff0c\u5b83\u7684\u8bed\u6cd5\u4e3a\uff1aselect &#8230;union allselect &#8230;\u3002union all \u4e0e union \u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u5b83\u4fdd\u7559\u91cd\u590d\u884c\uff0c\u7528\u4e8e\u9700\u8981\u5728\u7ed3\u679c\u4e2d\u4fdd\u6301\u91cd\u590d\u884c\u7684\u60c5\u51b5\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cunion all \u4e0d\u8fdb\u884c\u53bb\u91cd\uff0c\u56e0\u6b64\u53ef\u80fd\u5bfc\u81f4\u8f83\u5927\u7684\u7ed3\u679c\u96c6\uff0c\u5e76\u4e14\u9700\u8981\u786e\u4fdd\u7ec4\u5408\u7684 select \u8bed\u53e5\u5177\u6709\u76f8\u540c\u7684\u5217\u6570\u548c\u6570\u636e\u7c7b\u578b\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/26\/2024052618094783554.jpg\" class=\"aligncenter\" title=\"mysql\u4e2dunionall\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"mysql\u4e2dunionall\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>UNION ALL \u8bed\u6cd5<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\uff0cUNION ALL \u8fd0\u7b97\u7b26\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a SELECT \u8bed\u53e5\u7684\u7ed3\u679c\u7ec4\u5408\u6210\u4e00\u4e2a\u5355\u4e00\u7684\u8868\u3002\u5b83\u4e0d\u6267\u884c\u53bb\u91cd\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4f1a\u4fdd\u7559\u91cd\u590d\u884c\u3002<\/p>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<p>\u4f7f\u7528 UNION ALL \u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT ...\nUNION ALL\nSELECT ...<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a SELECT \u8bed\u53e5\uff0c\u53ea\u8981\u5b83\u4eec\u7684\u5217\u6570\u548c\u6570\u636e\u7c7b\u578b\u5339\u914d\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u5c06 customers \u548c orders \u8868\u4e2d\u7684\u6570\u636e\u7ec4\u5408\u6210\u4e00\u4e2a\u5355\u4e00\u7684\u8868\uff1a<\/p>\n<pre>SELECT * FROM customers\nUNION ALL\nSELECT * FROM orders;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7ed3\u679c\u8868\u5c06\u5305\u542b\u4e24\u5f20\u8868\u7684\u884c\uff0c\u5305\u62ec\u91cd\u590d\u884c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e24\u4e2a\u8868\u90fd\u6709\u4e00\u4e2a\u540d\u4e3a name \u7684\u5217\uff0c\u7ed3\u679c\u8868\u4e5f\u4f1a\u6709\u4e24\u4e2a\u540d\u4e3a name \u7684\u5217\u3002<\/p>\n<p><strong>\u4e0e UNION \u7684\u533a\u522b<\/strong><\/p>\n<p>UNION \u548c UNION ALL \u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\uff0cUNION \u4f1a\u53bb\u9664\u91cd\u590d\u884c\uff0c\u800c UNION ALL \u4e0d\u53bb\u9664\u91cd\u590d\u884c\u3002\u5982\u679c\u4f60\u5728\u7ed3\u679c\u4e2d\u9700\u8981\u4fdd\u6301\u91cd\u590d\u884c\uff0c\u8bf7\u4f7f\u7528 UNION ALL\u3002<\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>UNION ALL \u4e0d\u6267\u884c\u53bb\u91cd\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5bfc\u81f4\u8f83\u5927\u7684\u7ed3\u679c\u96c6\u3002<\/li>\n<li>\u786e\u4fdd\u6240\u7ec4\u5408\u7684 SELECT \u8bed\u53e5\u5177\u6709\u76f8\u540c\u7684\u5217\u6570\u548c\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li>\u5982\u679c\u4e24\u4e2a SELECT \u8bed\u53e5\u7684\u5217\u540d\u4e0d\u540c\uff0c\u5219\u7ed3\u679c\u8868\u4e2d\u7684\u5217\u5c06\u4f7f\u7528\u7b2c\u4e00\u4e2a SELECT \u8bed\u53e5\u7684\u5217\u540d\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e2dunionall\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 \u4e2d\u7684 union all \u8fd0\u7b97\u7b26\u7ec4\u5408\u591a\u4e2a select \u8bed\u53e5\u7684\u7ed3\u679c\uff0c\u800c\u4e0d\u53bb\u9664\u91cd\u590d\u884c\uff0c\u5b83\u7684\u8bed\u6cd5\u4e3a\uff1aselect &#8230;union allselect &#8230;\u3002union all \u4e0e union \u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u5b83\u4fdd\u7559\u91cd\u590d\u884c\uff0c\u7528\u4e8e\u9700\u8981\u5728\u7ed3\u679c\u4e2d\u4fdd\u6301\u91cd\u590d\u884c\u7684\u60c5\u51b5\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cunion all \u4e0d\u8fdb\u884c\u53bb\u91cd\uff0c\u56e0\u6b64\u53ef\u80fd\u5bfc\u81f4\u8f83\u5927\u7684\u7ed3\u679c\u96c6\uff0c\u5e76\u4e14\u9700\u8981\u786e\u4fdd\u7ec4\u5408\u7684 select \u8bed\u53e5\u5177\u6709\u76f8\u540c\u7684\u5217\u6570\u548c\u6570\u636e\u7c7b\u578b\u3002 UNION ALL \u8bed\u6cd5 \u5728 MySQL \u4e2d\uff0cUNION ALL \u8fd0\u7b97\u7b26\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a SELECT \u8bed\u53e5\u7684\u7ed3\u679c\u7ec4\u5408\u6210\u4e00\u4e2a\u5355\u4e00\u7684\u8868\u3002\u5b83\u4e0d\u6267\u884c\u53bb\u91cd\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4f1a\u4fdd\u7559\u91cd\u590d\u884c\u3002 \u7528\u6cd5 \u4f7f\u7528 UNION ALL \u7684\u8bed\u6cd5\u5982\u4e0b\uff1a SELECT &#8230; UNION ALL SELECT &#8230; \u767b\u5f55\u540e\u590d\u5236 \u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a SELECT \u8bed\u53e5\uff0c\u53ea\u8981\u5b83\u4eec\u7684\u5217\u6570\u548c\u6570\u636e\u7c7b\u578b\u5339\u914d\u3002 \u793a\u4f8b \u4ee5\u4e0b\u793a\u4f8b\u5c06 customers \u548c orders \u8868\u4e2d\u7684\u6570\u636e\u7ec4\u5408\u6210\u4e00\u4e2a\u5355\u4e00\u7684\u8868\uff1a SELECT * FROM customers UNION ALL [&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-5275","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5275","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=5275"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5275\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}