{"id":2657,"date":"2025-03-12T23:34:23","date_gmt":"2025-03-12T15:34:23","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2657"},"modified":"2025-03-12T23:34:23","modified_gmt":"2025-03-12T15:34:23","slug":"postgresql-union-%e6%93%8d%e4%bd%9c%e7%ac%a6%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/12\/postgresql-union-%e6%93%8d%e4%bd%9c%e7%ac%a6%e8%af%a6%e8%a7%a3\/","title":{"rendered":"PostgreSQL UNION \u64cd\u4f5c\u7b26\u8be6\u89e3"},"content":{"rendered":"\n<p>PostgreSQL <code>UNION<\/code> \u64cd\u4f5c\u7b26\u7528\u4e8e\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2a <code>SELECT<\/code> \u67e5\u8be2\u7684\u7ed3\u679c\u96c6\uff0c\u53bb\u9664\u91cd\u590d\u884c\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u7ed3\u679c\u96c6\u3002\u5b83\u7528\u4e8e\u4ece\u591a\u4e2a\u8868\u6216\u67e5\u8be2\u4e2d\u6574\u5408\u6570\u636e\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. UNION \u7684\u57fa\u672c\u7528\u6cd5<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2, ... FROM table1\nUNION\nSELECT column1, column2, ... FROM table2;<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u6ce8\u610f<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>UNION<\/code> \u9ed8\u8ba4\u4f1a <strong>\u53bb\u9664\u91cd\u590d\u6570\u636e<\/strong>\u3002<\/li>\n\n\n\n<li>\u6bcf\u4e2a <code>SELECT<\/code> \u8bed\u53e5\u7684\u5217\u6570\u548c\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u3002<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p>\u5047\u8bbe\u6709\u4e24\u4e2a\u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE employees (\n    id SERIAL PRIMARY KEY,\n    name VARCHAR(100),\n    department VARCHAR(50)\n);\n\nCREATE TABLE managers (\n    id SERIAL PRIMARY KEY,\n    name VARCHAR(100),\n    department VARCHAR(50)\n);<\/code><\/pre>\n\n\n\n<p>\u63d2\u5165\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO employees (name, department) VALUES \n('Alice', 'HR'), ('Bob', 'IT'), ('Charlie', 'Finance');\n\nINSERT INTO managers (name, department) VALUES \n('David', 'HR'), ('Eve', 'IT');<\/code><\/pre>\n\n\n\n<p>\u6267\u884c <code>UNION<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, department FROM employees\nUNION\nSELECT name, department FROM managers;<\/code><\/pre>\n\n\n\n<p><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>name<\/th><th>department<\/th><\/tr><\/thead><tbody><tr><td>Alice<\/td><td>HR<\/td><\/tr><tr><td>Bob<\/td><td>IT<\/td><\/tr><tr><td>Charlie<\/td><td>Finance<\/td><\/tr><tr><td>David<\/td><td>HR<\/td><\/tr><tr><td>Eve<\/td><td>IT<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. UNION ALL\uff08\u4fdd\u7559\u91cd\u590d\u6570\u636e\uff09<\/strong><\/h2>\n\n\n\n<p><code>UNION ALL<\/code> \u548c <code>UNION<\/code> \u7684\u533a\u522b\u5728\u4e8e\uff0c\u5b83 <strong>\u4e0d\u4f1a\u53bb\u9664\u91cd\u590d\u884c<\/strong>\uff0c\u5408\u5e76\u540e\u6240\u6709\u6570\u636e\u90fd\u4f1a\u4fdd\u7559\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2, ... FROM table1\nUNION ALL\nSELECT column1, column2, ... FROM table2;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, department FROM employees\nUNION ALL\nSELECT name, department FROM managers;<\/code><\/pre>\n\n\n\n<p><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>name<\/th><th>department<\/th><\/tr><\/thead><tbody><tr><td>Alice<\/td><td>HR<\/td><\/tr><tr><td>Bob<\/td><td>IT<\/td><\/tr><tr><td>Charlie<\/td><td>Finance<\/td><\/tr><tr><td>David<\/td><td>HR<\/td><\/tr><tr><td>Eve<\/td><td>IT<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5982\u679c <code>employees<\/code> \u8868\u4e2d\u4e5f\u6709 <code>David<\/code>\uff0c\u90a3\u4e48 <code>UNION<\/code> \u4f1a\u53bb\u91cd\uff0c\u800c <code>UNION ALL<\/code> \u4f1a\u4fdd\u7559\u591a\u4e2a <code>David<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. UNION \u7684\u6392\u5e8f<\/strong><\/h2>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>ORDER BY<\/code> \u5bf9\u6700\u7ec8\u5408\u5e76\u7684\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, department FROM employees\nUNION\nSELECT name, department FROM managers\nORDER BY name ASC;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. UNION \u5728 WHERE \u8fc7\u6ee4\u6761\u4ef6\u4e2d\u7684\u5e94\u7528<\/strong><\/h2>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>WHERE<\/code> \u8bed\u53e5\u5728\u5408\u5e76\u6570\u636e\u524d\u8fdb\u884c\u7b5b\u9009\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, department FROM employees WHERE department = 'IT'\nUNION\nSELECT name, department FROM managers WHERE department = 'IT';<\/code><\/pre>\n\n\n\n<p><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>name<\/th><th>department<\/th><\/tr><\/thead><tbody><tr><td>Bob<\/td><td>IT<\/td><\/tr><tr><td>Eve<\/td><td>IT<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. UNION \u4e0e\u4e0d\u540c\u6570\u636e\u5217\u6570<\/strong><\/h2>\n\n\n\n<p><code>UNION<\/code> \u64cd\u4f5c\u7684 <code>SELECT<\/code> \u8bed\u53e5\u5fc5\u987b\u6709\u76f8\u540c\u7684\u5217\u6570\uff0c\u5982\u679c\u4e0d\u5339\u914d\uff0c\u5219\u4f1a\u62a5\u9519\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, department FROM employees\nUNION\nSELECT name FROM managers;  -- \u9519\u8bef\uff1a\u5217\u6570\u4e0d\u5339\u914d<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u60f3\u8981\u5408\u5e76\u4e0d\u540c\u5217\u6570\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>NULL<\/code> \u4f5c\u4e3a\u5360\u4f4d\u7b26\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, department FROM employees\nUNION\nSELECT name, NULL FROM managers;<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837 <code>department<\/code> \u5728 <code>managers<\/code> \u8868\u4e2d\u4f1a\u663e\u793a <code>NULL<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. UNION \u5728\u5b50\u67e5\u8be2\u4e2d\u7684\u5e94\u7528<\/strong><\/h2>\n\n\n\n<p>\u53ef\u4ee5\u5728\u5b50\u67e5\u8be2\u4e2d\u4f7f\u7528 <code>UNION<\/code> \u8fdb\u884c\u6570\u636e\u5408\u5e76\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, department FROM (\n    SELECT name, department FROM employees\n    UNION\n    SELECT name, department FROM managers\n) AS combined\nWHERE department = 'HR';<\/code><\/pre>\n\n\n\n<p><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>name<\/th><th>department<\/th><\/tr><\/thead><tbody><tr><td>Alice<\/td><td>HR<\/td><\/tr><tr><td>David<\/td><td>HR<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u64cd\u4f5c\u7b26<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>UNION<\/code><\/td><td>\u5408\u5e76\u67e5\u8be2\u7ed3\u679c\u5e76\u53bb\u91cd<\/td><\/tr><tr><td><code>UNION ALL<\/code><\/td><td>\u5408\u5e76\u67e5\u8be2\u7ed3\u679c\u4f46\u4e0d\u53bb\u91cd<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL UNION \u64cd\u4f5c\u7b26\u7528\u4e8e\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2a SELECT \u67e5\u8be2\u7684\u7ed3\u679c\u96c6\uff0c\u53bb\u9664\u91cd\u590d\u884c\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2658,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-2657","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2657"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2657\/revisions"}],"predecessor-version":[{"id":2659,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2657\/revisions\/2659"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2658"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}