{"id":5881,"date":"2024-11-14T08:00:39","date_gmt":"2024-11-14T00:00:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5881\/"},"modified":"2024-11-14T08:00:39","modified_gmt":"2024-11-14T00:00:39","slug":"sql%e8%af%ad%e5%8f%a5union%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5881\/","title":{"rendered":"sql\u8bed\u53e5union\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  union\u64cd\u4f5c\u7528\u4e8e\u5408\u5e76\u591a\u8868\u8bb0\u5f55\u81f3\u65b0\u7ed3\u679c\u96c6\u4e2d\uff0c\u8bed\u6cd5\u4e3a\uff1aselect column_list from table1 union [select column_list from table2]&#8230;\u3002\u6b64\u64cd\u4f5c\u4f1a\u81ea\u52a8\u6392\u9664\u91cd\u590d\u8bb0\u5f55\uff0c\u5e76\u53ef\u5408\u5e76\u62e5\u6709\u76f8\u540c\u7ed3\u6784\u548c\u5217\u540d\u7684\u6765\u81ea\u4e0d\u540c\u8868\u7684\u76f8\u5173\u6570\u636e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060321340340911.jpg\" class=\"aligncenter\" title=\"sql\u8bed\u53e5union\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"sql\u8bed\u53e5union\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>UNION \u64cd\u4f5c<\/strong><\/p>\n<p>UNION \u64cd\u4f5c\u7528\u4e8e\u5408\u5e76\u4e24\u4e2a\u6216\u66f4\u591a\u8868\u4e2d\u7684\u8bb0\u5f55\uff0c\u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u7ed3\u679c\u96c6\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u7ec4\u5408\u5177\u6709\u76f8\u540c\u7ed3\u6784\u4f46\u6765\u81ea\u4e0d\u540c\u8868\u7684\u76f8\u5173\u6570\u636e\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>SELECT column_list\nFROM table1\nUNION\nSELECT column_list\nFROM table2\n[UNION\nSELECT column_list\nFROM table3\n...];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8bf4\u660e<\/strong><\/p>\n<ul>\n<li>column_list \u6307\u5b9a\u8981\u4ece\u6bcf\u4e2a\u8868\u4e2d\u9009\u53d6\u7684\u5217\u3002<\/li>\n<li>\u5982\u679c\u4e24\u4e2a\u6216\u66f4\u591a\u8868\u4e2d\u7684\u5217\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\u548c\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u4eec\u5c06\u88ab\u7ec4\u5408\u5230\u7ed3\u679c\u96c6\u4e2d\u7684\u76f8\u5e94\u5217\u4e2d\u3002<\/li>\n<li>UNION \u64cd\u4f5c\u4f1a\u81ea\u52a8\u5220\u9664\u91cd\u590d\u7684\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868 customers \u548c orders\uff0c\u5b83\u4eec\u7684\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre>CREATE TABLE customers (\n  customer_id INT NOT NULL,\n  customer_name VARCHAR(255) NOT NULL,\n  CONSTRAINT PK_customers PRIMARY KEY (customer_id)\n);\n\nCREATE TABLE orders (\n  order_id INT NOT NULL,\n  customer_id INT NOT NULL,\n  order_date DATE NOT NULL,\n  CONSTRAINT FK_orders_customers FOREIGN KEY (customer_id) REFERENCES customers (customer_id)\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5728\u4ee5\u4e0b\u67e5\u8be2\u4e2d\u4f7f\u7528 UNION \u64cd\u4f5c\u6765\u83b7\u53d6\u6240\u6709\u5ba2\u6237\u548c\u8ba2\u5355\u4fe1\u606f\uff1a<\/p>\n<pre>SELECT *\nFROM customers\nUNION\nSELECT customer_id, customer_name, order_date\nFROM orders;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7ed3\u679c\u96c6\u5c06\u5305\u542b\u6240\u6709\u5ba2\u6237\u8bb0\u5f55\uff0c\u4ee5\u53ca\u6bcf\u4e2a\u5ba2\u6237\u5173\u8054\u7684\u8ba2\u5355\u8bb0\u5f55\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662funion\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>union\u64cd\u4f5c\u7528\u4e8e\u5408\u5e76\u591a\u8868\u8bb0\u5f55\u81f3\u65b0\u7ed3\u679c\u96c6\u4e2d\uff0c\u8bed\u6cd5\u4e3a\uff1aselect column_list from table1 union [select column_list from table2]&#8230;\u3002\u6b64\u64cd\u4f5c\u4f1a\u81ea\u52a8\u6392\u9664\u91cd\u590d\u8bb0\u5f55\uff0c\u5e76\u53ef\u5408\u5e76\u62e5\u6709\u76f8\u540c\u7ed3\u6784\u548c\u5217\u540d\u7684\u6765\u81ea\u4e0d\u540c\u8868\u7684\u76f8\u5173\u6570\u636e\u3002 UNION \u64cd\u4f5c UNION \u64cd\u4f5c\u7528\u4e8e\u5408\u5e76\u4e24\u4e2a\u6216\u66f4\u591a\u8868\u4e2d\u7684\u8bb0\u5f55\uff0c\u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u7ed3\u679c\u96c6\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u7ec4\u5408\u5177\u6709\u76f8\u540c\u7ed3\u6784\u4f46\u6765\u81ea\u4e0d\u540c\u8868\u7684\u76f8\u5173\u6570\u636e\u3002 \u8bed\u6cd5 SELECT column_list FROM table1 UNION SELECT column_list FROM table2 [UNION SELECT column_list FROM table3 &#8230;]; \u767b\u5f55\u540e\u590d\u5236 \u8bf4\u660e column_list \u6307\u5b9a\u8981\u4ece\u6bcf\u4e2a\u8868\u4e2d\u9009\u53d6\u7684\u5217\u3002 \u5982\u679c\u4e24\u4e2a\u6216\u66f4\u591a\u8868\u4e2d\u7684\u5217\u5177\u6709\u76f8\u540c\u7684\u540d\u79f0\u548c\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u4eec\u5c06\u88ab\u7ec4\u5408\u5230\u7ed3\u679c\u96c6\u4e2d\u7684\u76f8\u5e94\u5217\u4e2d\u3002 UNION \u64cd\u4f5c\u4f1a\u81ea\u52a8\u5220\u9664\u91cd\u590d\u7684\u8bb0\u5f55\u3002 \u4f7f\u7528\u793a\u4f8b \u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868 customers \u548c orders\uff0c\u5b83\u4eec\u7684\u7ed3\u6784\u5982\u4e0b\uff1a CREATE TABLE customers ( customer_id INT NOT NULL, customer_name VARCHAR(255) NOT NULL, CONSTRAINT PK_customers [&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-5881","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5881","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=5881"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5881\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}