{"id":136643,"date":"2024-10-31T11:37:24","date_gmt":"2024-10-31T03:37:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/136643\/"},"modified":"2024-10-31T11:37:25","modified_gmt":"2024-10-31T03:37:25","slug":"sql-server-2008%e5%ad%b8%e7%bf%92%e7%ad%86%e8%a8%98%e4%b9%8b%e8%81%af%e5%90%88%e7%b5%90%e6%9e%9c%e9%9b%86union%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/136643\/","title":{"rendered":"SQL Server 2008\u5b78\u7fd2\u7b46\u8a18\u4e4b\u806f\u5408\u7d50\u679c\u96c6union\u7684\u4f7f\u7528"},"content":{"rendered":"<h1 id=\"sql-server-2008%e5%ad%b8%e7%bf%92%e7%ad%86%e8%a8%98%e4%b9%8b%e8%81%af%e5%90%88%e7%b5%90%e6%9e%9c%e9%9b%86union%e7%9a%84%e4%bd%bf%e7%94%a8-ogJIzAhGds\">SQL Server 2008\u5b78\u7fd2\u7b46\u8a18\u4e4b\u806f\u5408\u7d50\u679c\u96c6UNION\u7684\u4f7f\u7528<\/h1>\n<p>\u5728\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0cSQL Server 2008\u662f\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u7248\u672c\uff0c\u63d0\u4f9b\u4e86\u8a31\u591a\u5f37\u5927\u7684\u529f\u80fd\u4f86\u8655\u7406\u548c\u67e5\u8a62\u8cc7\u6599\u3002\u672c\u6587\u5c07\u91cd\u9ede\u4ecb\u7d39SQL Server 2008\u4e2d\u7684\u806f\u5408\u7d50\u679c\u96c6\uff08UNION\uff09\u7684\u4f7f\u7528\uff0c\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u6709\u6548\u5730\u6574\u5408\u4f86\u81ea\u4e0d\u540c\u67e5\u8a62\u7684\u7d50\u679c\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afunion-ogJIzAhGds\">\u4ec0\u9ebc\u662fUNION?<\/h2>\n<p>UNION\u662f\u4e00\u500bSQL\u64cd\u4f5c\u7b26\uff0c\u7528\u65bc\u5c07\u5169\u500b\u6216\u591a\u500bSELECT\u67e5\u8a62\u7684\u7d50\u679c\u5408\u4f75\u6210\u4e00\u500b\u7d50\u679c\u96c6\u3002\u4f7f\u7528UNION\u6642\uff0c\u6240\u6709\u67e5\u8a62\u7684\u5217\u6578\u548c\u6578\u64da\u985e\u578b\u5fc5\u9808\u76f8\u540c\u3002UNION\u64cd\u4f5c\u7b26\u6703\u81ea\u52d5\u53bb\u9664\u91cd\u8907\u7684\u8a18\u9304\uff0c\u82e5\u5e0c\u671b\u4fdd\u7559\u91cd\u8907\u7684\u8a18\u9304\uff0c\u53ef\u4ee5\u4f7f\u7528UNION ALL\u3002<\/p>\n<h2 id=\"union%e7%9a%84%e5%9f%ba%e6%9c%ac%e8%aa%9e%e6%b3%95-ogJIzAhGds\">UNION\u7684\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<pre><code>\nSELECT column1, column2, ...\nFROM table1\nWHERE condition\nUNION\nSELECT column1, column2, ...\nFROM table2\nWHERE condition;\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u8a9e\u6cd5\u4e2d\uff0c\u7b2c\u4e00\u500bSELECT\u67e5\u8a62\u7684\u7d50\u679c\u5c07\u8207\u7b2c\u4e8c\u500bSELECT\u67e5\u8a62\u7684\u7d50\u679c\u5408\u4f75\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f7f\u7528UNION\u7684\u5be6\u969b\u7bc4\u4f8b\u3002<\/p>\n<h2 id=\"%e7%af%84%e4%be%8b%ef%bc%9a%e4%bd%bf%e7%94%a8union%e5%90%88%e4%bd%b5%e6%9f%a5%e8%a9%a2%e7%b5%90%e6%9e%9c-ogJIzAhGds\">\u7bc4\u4f8b\uff1a\u4f7f\u7528UNION\u5408\u4f75\u67e5\u8a62\u7d50\u679c<\/h2>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u5169\u500b\u8868\u683c\uff1a<strong>Customers<\/strong>\u548c<strong>Suppliers<\/strong>\uff0c\u5b83\u5011\u90fd\u6709\u76f8\u540c\u7684\u6b04\u4f4d\uff1a<strong>Name<\/strong>\u548c<strong>City<\/strong>\u3002\u6211\u5011\u5e0c\u671b\u7372\u53d6\u6240\u6709\u4f86\u81ea\u9019\u5169\u500b\u8868\u683c\u7684\u540d\u7a31\u548c\u57ce\u5e02\u3002<\/p>\n<pre><code>\nSELECT Name, City\nFROM Customers\nWHERE City = 'Hong Kong'\nUNION\nSELECT Name, City\nFROM Suppliers\nWHERE City = 'Hong Kong';\n<\/code><\/pre>\n<p>\u9019\u500b\u67e5\u8a62\u5c07\u8fd4\u56de\u6240\u6709\u4f86\u81ea\u9999\u6e2f\u7684\u5ba2\u6236\u548c\u4f9b\u61c9\u5546\u7684\u540d\u7a31\u548c\u57ce\u5e02\uff0c\u4e26\u4e14\u4e0d\u6703\u6709\u91cd\u8907\u7684\u8a18\u9304\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8union-all%e4%bf%9d%e7%95%99%e9%87%8d%e8%a4%87%e8%a8%98%e9%8c%84-ogJIzAhGds\">\u4f7f\u7528UNION ALL\u4fdd\u7559\u91cd\u8907\u8a18\u9304<\/h2>\n<p>\u5982\u679c\u6211\u5011\u5e0c\u671b\u4fdd\u7559\u91cd\u8907\u7684\u8a18\u9304\uff0c\u53ef\u4ee5\u4f7f\u7528UNION ALL\u3002\u4ee5\u4e0b\u662f\u76f8\u540c\u67e5\u8a62\u7684\u7bc4\u4f8b\uff0c\u4f46\u9019\u6b21\u4f7f\u7528UNION ALL\u3002<\/p>\n<pre><code>\nSELECT Name, City\nFROM Customers\nWHERE City = 'Hong Kong'\nUNION ALL\nSELECT Name, City\nFROM Suppliers\nWHERE City = 'Hong Kong';\n<\/code><\/pre>\n<p>\u9019\u6a23\u7684\u67e5\u8a62\u5c07\u8fd4\u56de\u6240\u6709\u4f86\u81ea\u9999\u6e2f\u7684\u5ba2\u6236\u548c\u4f9b\u61c9\u5546\u7684\u540d\u7a31\u548c\u57ce\u5e02\uff0c\u5305\u62ec\u91cd\u8907\u7684\u8a18\u9304\u3002<\/p>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-ogJIzAhGds\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li>\u6240\u6709SELECT\u67e5\u8a62\u7684\u5217\u6578\u5fc5\u9808\u76f8\u540c\u3002<\/li>\n<li>\u5c0d\u61c9\u7684\u5217\u6578\u64da\u985e\u578b\u5fc5\u9808\u517c\u5bb9\u3002<\/li>\n<li>UNION\u64cd\u4f5c\u7b26\u6703\u81ea\u52d5\u53bb\u9664\u91cd\u8907\u7684\u8a18\u9304\uff0c\u800cUNION ALL\u5247\u4e0d\u6703\u3002<\/li>\n<li>\u4f7f\u7528UNION\u6642\uff0c\u67e5\u8a62\u7684\u57f7\u884c\u6548\u7387\u53ef\u80fd\u6703\u53d7\u5230\u5f71\u97ff\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-ogJIzAhGds\">\u7d50\u8ad6<\/h2>\n<p>UNION\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5728SQL Server 2008\u4e2d\u6709\u6548\u5730\u6574\u5408\u4f86\u81ea\u4e0d\u540c\u4f86\u6e90\u7684\u6578\u64da\u3002\u901a\u904e\u6b63\u78ba\u4f7f\u7528UNION\u548cUNION ALL\uff0c\u60a8\u53ef\u4ee5\u9748\u6d3b\u5730\u8655\u7406\u67e5\u8a62\u7d50\u679c\uff0c\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u5982\u4f55\u5728\u96f2\u7aef\u74b0\u5883\u4e2d\u904b\u884cSQL Server\u6709\u8208\u8da3\uff0c\u6216\u662f\u9700\u8981\u9ad8\u6548\u80fd\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22SQL Server 2008\u4e2d\u806f\u5408\u7d50\u679c\u96c6UNION\u7684\u4f7f\u7528\u6280\u5de7\uff0c\u63d0\u5347\u6578\u64da\u67e5\u8a62\u6548\u7387\uff0c\u7c21\u5316\u591a\u67e5\u8a62\u7d50\u679c\u6574\u5408\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-136643","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/136643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=136643"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/136643\/revisions"}],"predecessor-version":[{"id":136644,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/136643\/revisions\/136644"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=136643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=136643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=136643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}