{"id":2354,"date":"2025-03-04T19:30:48","date_gmt":"2025-03-04T11:30:48","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2354"},"modified":"2025-03-04T19:30:48","modified_gmt":"2025-03-04T11:30:48","slug":"union-%e6%9f%a5%e8%af%a2%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/04\/union-%e6%9f%a5%e8%af%a2%e7%a4%ba%e4%be%8b\/","title":{"rendered":"UNION\u00a0\u67e5\u8be2\u793a\u4f8b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5982\u4e0b\u5217\u4e3e\u4e86\u51e0\u4e2a <code>UNION<\/code> \u67e5\u8be2\u793a\u4f8b\uff0c\u5e94\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u6db5\u76d6\u4e0d\u540c\u7684\u5b9e\u9645\u9700\u6c42\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. <code>UNION<\/code> \u7ed3\u5408 <code>GROUP BY<\/code> \u8fdb\u884c\u6570\u636e\u7edf\u8ba1<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u6211\u4eec\u6709\u4e24\u4e2a\u4e0d\u540c\u7684\u9500\u552e\u8868\uff0c\u60f3\u8981\u7edf\u8ba1\u4e0d\u540c\u5730\u533a\u7684\u9500\u552e\u603b\u989d\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u5408\u5e76\u4e0d\u540c\u8868\u7684\u9500\u552e\u6570\u636e\uff0c\u5e76\u8ba1\u7b97\u603b\u989d<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE sales_2023 (\n    region TEXT,\n    total_sales INTEGER\n);\n\nCREATE TABLE sales_2024 (\n    region TEXT,\n    total_sales INTEGER\n);\n\nINSERT INTO sales_2023 VALUES ('North', 5000), ('South', 7000);\nINSERT INTO sales_2024 VALUES ('North', 8000), ('West', 6000);\n\nSELECT region, SUM(total_sales) AS total_revenue\nFROM (\n    SELECT region, total_sales FROM sales_2023\n    UNION\n    SELECT region, total_sales FROM sales_2024\n)\nGROUP BY region;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>region | total_revenue\n----------------------\nNorth  | 13000\nSouth  | 7000\nWest   | 6000<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u91ca<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5148\u7528 <code>UNION<\/code> \u5408\u5e76\u4e24\u5e74\u6570\u636e\uff0c\u518d\u7528 <code>GROUP BY<\/code> \u7edf\u8ba1\u6bcf\u4e2a <code>region<\/code> \u7684\u603b\u9500\u552e\u989d\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. <code>UNION ALL<\/code> \u7ed3\u5408 <code>COUNT(*)<\/code> \u7edf\u8ba1\u6570\u636e\u91cf<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u5408\u5e76\u591a\u4e2a\u8868\u7684\u6570\u636e\uff0c\u5e76\u7edf\u8ba1\u6bcf\u4e2a\u6765\u6e90\u7684\u6570\u636e\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT '2023' AS year, COUNT(*) AS total_records FROM sales_2023\nUNION ALL\nSELECT '2024', COUNT(*) FROM sales_2024;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>year  | total_records\n----------------------\n2023  | 2\n2024  | 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u91ca<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u91cc\u6211\u4eec\u52a0\u4e86 <code>UNION ALL<\/code>\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u9700\u8981\u53bb\u91cd\uff0c\u53ea\u662f\u7edf\u8ba1\u5404\u5e74\u4efd\u7684\u8bb0\u5f55\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. <code>UNION<\/code> \u7ed3\u5408 <code>CASE<\/code> \u8fdb\u884c\u6570\u636e\u5206\u7c7b<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u5408\u5e76\u4e0d\u540c\u7684\u6570\u636e\u8868\uff0c\u5e76\u4e14\u4e3a\u4e0d\u540c\u7684\u6570\u636e\u52a0\u4e0a\u5206\u7c7b\u6807\u7b7e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, 'Employee' AS category FROM employees\nUNION\nSELECT name, 'Manager' FROM managers;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name    | category\n-------------------\nAlice   | Employee\nBob     | Employee\nCharlie | Manager\nDavid   | Manager<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u91ca<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>UNION<\/code> \u8ba9 <code>employees<\/code> \u548c <code>managers<\/code> \u6570\u636e\u6574\u5408\u5230\u4e00\u8d77\uff0c\u5e76\u901a\u8fc7 <code>CASE<\/code> \u8bed\u53e5\u52a0\u4e0a\u7c7b\u522b\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. <code>UNION<\/code> \u7ed3\u5408 <code>JOIN<\/code> \u8fdb\u884c\u591a\u8868\u67e5\u8be2<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u6709\u591a\u4e2a\u8ba2\u5355\u8868\uff0c\u60f3\u8981\u83b7\u53d6\u6240\u6709\u7528\u6237\u7684\u8ba2\u5355\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT users.name, orders_2023.product\nFROM users\nINNER JOIN orders_2023 ON users.id = orders_2023.user_id\n\nUNION\n\nSELECT users.name, orders_2024.product\nFROM users\nINNER JOIN orders_2024 ON users.id = orders_2024.user_id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u91ca<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>JOIN<\/code> \u5148\u5173\u8054 <code>users<\/code> \u548c <code>orders_xxxx<\/code> \u8868\uff0c\u518d\u7528 <code>UNION<\/code> \u5408\u5e76\u4e0d\u540c\u5e74\u4efd\u7684\u8ba2\u5355\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. <code>UNION<\/code> \u7ed3\u5408 <code>LIMIT<\/code> \u8fdb\u884c\u5206\u9875<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u5728\u5408\u5e76\u6570\u636e\u540e<strong>\u9650\u5236\u8fd4\u56de\u884c\u6570<\/strong>\uff08\u5206\u9875\uff09\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name FROM users1\nUNION\nSELECT name FROM users2\nORDER BY name\nLIMIT 5 OFFSET 10;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u91ca<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>LIMIT 5 OFFSET 10<\/code>\uff1a\u8df3\u8fc7\u524d 10 \u884c\uff0c\u8fd4\u56de 5 \u884c\u6570\u636e\uff0c\u9002\u7528\u4e8e<strong>\u5206\u9875\u67e5\u8be2<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. <code>UNION<\/code> \u7ed3\u5408 <code>HAVING<\/code> \u8fc7\u6ee4\u6570\u636e<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u5728 <code>UNION<\/code> \u540e\u6839\u636e\u67d0\u4e2a\u8ba1\u7b97\u503c\u8fdb\u884c\u8fc7\u6ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT region, SUM(total_sales) AS revenue\nFROM (\n    SELECT region, total_sales FROM sales_2023\n    UNION\n    SELECT region, total_sales FROM sales_2024\n)\nGROUP BY region\nHAVING revenue &gt; 6000;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>region | revenue\n----------------------\nNorth  | 13000\nSouth  | 7000<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u91ca<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>HAVING revenue > 6000<\/code> \u53ea\u4fdd\u7559\u603b\u9500\u552e\u989d\u5927\u4e8e 6000 \u7684\u5730\u533a\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7528\u6cd5<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>UNION<\/code> + <code>GROUP BY<\/code><\/td><td>\u7edf\u8ba1\u5408\u5e76\u6570\u636e\u7684\u603b\u548c\u3001\u5747\u503c\u7b49<\/td><\/tr><tr><td><code>UNION ALL<\/code> + <code>COUNT(*)<\/code><\/td><td>\u8ba1\u7b97\u6570\u636e\u91cf\uff0c\u4e0d\u53bb\u91cd<\/td><\/tr><tr><td><code>UNION<\/code> + <code>CASE<\/code><\/td><td>\u7ed9\u4e0d\u540c\u6765\u6e90\u6570\u636e\u52a0\u6807\u7b7e<\/td><\/tr><tr><td><code>UNION<\/code> + <code>JOIN<\/code><\/td><td>\u5408\u5e76\u591a\u5f20\u8868\u7684\u5173\u8054\u6570\u636e<\/td><\/tr><tr><td><code>UNION<\/code> + <code>LIMIT<\/code><\/td><td>\u5b9e\u73b0\u5206\u9875\u67e5\u8be2<\/td><\/tr><tr><td><code>UNION<\/code> + <code>HAVING<\/code><\/td><td>\u8fdb\u884c\u7b5b\u9009\uff0c\u4f8b\u5982\u7edf\u8ba1\u8d85\u8fc7\u4e00\u5b9a\u91d1\u989d\u7684\u8ba2\u5355<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4e0b\u5217\u4e3e\u4e86\u51e0\u4e2a UNION \u67e5\u8be2\u793a\u4f8b\uff0c\u5e94\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u6db5\u76d6\u4e0d\u540c\u7684\u5b9e\u9645\u9700\u6c42\u3002 1. UNION \u7ed3\u5408 GROU [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-2354","post","type-post","status-publish","format-standard","hentry","category-sqlite"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2354","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=2354"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2354\/revisions"}],"predecessor-version":[{"id":2355,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2354\/revisions\/2355"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}