{"id":160281,"date":"2024-11-03T21:47:16","date_gmt":"2024-11-03T13:47:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/160281\/"},"modified":"2024-11-03T21:47:17","modified_gmt":"2024-11-03T13:47:17","slug":"oceanbase-%e6%95%99%e7%a8%8b%ef%bc%9aoceanbase-union","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/160281\/","title":{"rendered":"OceanBase \u6559\u7a0b\uff1aOceanBase UNION"},"content":{"rendered":"<h1 id=\"oceanbase-%e6%95%99%e7%a8%8b%ef%bc%9aoceanbase-union-OpBGHkGizf\">OceanBase \u6559\u7a0b\uff1aOceanBase UNION<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0cOceanBase \u4f5c\u70ba\u4e00\u500b\u9ad8\u6027\u80fd\u7684\u5206\u4f48\u5f0f\u6578\u64da\u5eab\uff0c\u8d8a\u4f86\u8d8a\u53d7\u5230\u958b\u767c\u8005\u548c\u4f01\u696d\u7684\u9752\u775e\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e OceanBase \u4e2d\u7684 UNION \u64cd\u4f5c\uff0c\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3\u5176\u7528\u6cd5\u53ca\u61c9\u7528\u5834\u666f\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-union%ef%bc%9f-OpBGHkGizf\">\u4ec0\u9ebc\u662f UNION\uff1f<\/h2>\n<p>UNION \u662f SQL \u4e2d\u7684\u4e00\u500b\u64cd\u4f5c\u7b26\uff0c\u7528\u65bc\u5c07\u5169\u500b\u6216\u591a\u500b SELECT \u67e5\u8a62\u7684\u7d50\u679c\u5408\u4f75\u6210\u4e00\u500b\u7d50\u679c\u96c6\u3002\u4f7f\u7528 UNION \u6642\uff0c\u6240\u6709\u7684 SELECT \u67e5\u8a62\u5fc5\u9808\u5177\u6709\u76f8\u540c\u7684\u5217\u6578\u548c\u76f8\u4f3c\u7684\u6578\u64da\u985e\u578b\u3002UNION \u64cd\u4f5c\u6703\u81ea\u52d5\u53bb\u9664\u91cd\u8907\u7684\u8a18\u9304\uff0c\u82e5\u9700\u8981\u4fdd\u7559\u91cd\u8907\u7684\u8a18\u9304\uff0c\u53ef\u4ee5\u4f7f\u7528 UNION ALL\u3002<\/p>\n<h2 id=\"oceanbase-%e4%b8%ad%e7%9a%84-union-%e8%aa%9e%e6%b3%95-OpBGHkGizf\">OceanBase \u4e2d\u7684 UNION \u8a9e\u6cd5<\/h2>\n<p>\u5728 OceanBase \u4e2d\uff0cUNION \u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>SELECT column1, column2, ...\nFROM table1\nWHERE condition1\nUNION\nSELECT column1, column2, ...\nFROM table2\nWHERE condition2;<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0ccolumn1 \u548c column2 \u662f\u8981\u9078\u64c7\u7684\u5217\uff0ctable1 \u548c table2 \u662f\u8981\u67e5\u8a62\u7684\u8868\uff0ccondition1 \u548c condition2 \u662f\u67e5\u8a62\u7684\u689d\u4ef6\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8-union-%e7%9a%84%e7%a4%ba%e4%be%8b-OpBGHkGizf\">\u4f7f\u7528 UNION \u7684\u793a\u4f8b<\/h2>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u5169\u500b\u8868\uff1aemployees \u548c contractors\uff0c\u5169\u8005\u90fd\u6709\u76f8\u540c\u7684\u5217\uff1aname \u548c role\u3002\u6211\u5011\u5e0c\u671b\u7372\u53d6\u6240\u6709\u7684\u54e1\u5de5\u548c\u627f\u5305\u5546\u7684\u59d3\u540d\u53ca\u89d2\u8272\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT name, role\nFROM employees\nUNION\nSELECT name, role\nFROM contractors;<\/code><\/pre>\n<p>\u9019\u500b\u67e5\u8a62\u5c07\u8fd4\u56de\u6240\u6709\u54e1\u5de5\u548c\u627f\u5305\u5546\u7684\u59d3\u540d\u548c\u89d2\u8272\uff0c\u4e26\u81ea\u52d5\u53bb\u9664\u91cd\u8907\u7684\u8a18\u9304\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8-union-all-%e7%9a%84%e7%a4%ba%e4%be%8b-OpBGHkGizf\">\u4f7f\u7528 UNION ALL \u7684\u793a\u4f8b<\/h3>\n<p>\u5982\u679c\u6211\u5011\u5e0c\u671b\u4fdd\u7559\u91cd\u8907\u7684\u8a18\u9304\uff0c\u53ef\u4ee5\u4f7f\u7528 UNION ALL\u3002\u4ee5\u4e0b\u662f\u76f8\u540c\u7684\u67e5\u8a62\uff0c\u4f46\u4f7f\u7528 UNION ALL\uff1a<\/p>\n<pre><code>SELECT name, role\nFROM employees\nUNION ALL\nSELECT name, role\nFROM contractors;<\/code><\/pre>\n<p>\u9019\u6a23\u7684\u67e5\u8a62\u5c07\u8fd4\u56de\u6240\u6709\u7684\u59d3\u540d\u548c\u89d2\u8272\uff0c\u5305\u62ec\u91cd\u8907\u7684\u8a18\u9304\u3002<\/p>\n<h2 id=\"union-%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-OpBGHkGizf\">UNION \u7684\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li><strong>\u5217\u6578\u548c\u6578\u64da\u985e\u578b\uff1a<\/strong>\u6240\u6709\u7684 SELECT \u67e5\u8a62\u5fc5\u9808\u8fd4\u56de\u76f8\u540c\u6578\u91cf\u7684\u5217\uff0c\u4e14\u5c0d\u61c9\u5217\u7684\u6578\u64da\u985e\u578b\u5fc5\u9808\u76f8\u4f3c\u3002<\/li>\n<li><strong>\u6392\u5e8f\uff1a<\/strong>\u5982\u679c\u9700\u8981\u5c0d\u6700\u7d42\u7d50\u679c\u9032\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u5728\u6700\u5f8c\u4e00\u500b SELECT \u67e5\u8a62\u5f8c\u4f7f\u7528 ORDER BY \u5b50\u53e5\u3002<\/li>\n<li><strong>\u6027\u80fd\u8003\u91cf\uff1a<\/strong>\u4f7f\u7528 UNION \u53ef\u80fd\u6703\u5f71\u97ff\u67e5\u8a62\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\uff0c\u5efa\u8b70\u6839\u64da\u5be6\u969b\u9700\u6c42\u9078\u64c7\u4f7f\u7528 UNION \u6216 UNION ALL\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-OpBGHkGizf\">\u7d50\u8ad6<\/h2>\n<p>UNION \u662f OceanBase \u4e2d\u4e00\u500b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u80fd\u5920\u6709\u6548\u5730\u5408\u4f75\u591a\u500b\u67e5\u8a62\u7684\u7d50\u679c\u3002\u901a\u904e\u6b63\u78ba\u4f7f\u7528 UNION \u548c UNION ALL\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9748\u6d3b\u5730\u8655\u7406\u6578\u64da\uff0c\u6eff\u8db3\u4e0d\u540c\u7684\u696d\u52d9\u9700\u6c42\u3002\u4e86\u89e3\u9019\u4e9b\u57fa\u672c\u6982\u5ff5\u5f8c\uff0c\u60a8\u53ef\u4ee5\u5728 OceanBase \u4e2d\u66f4\u9ad8\u6548\u5730\u9032\u884c\u6578\u64da\u67e5\u8a62\u548c\u7ba1\u7406\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 OceanBase \u6216\u5176\u4ed6\u6578\u64da\u5eab\u6280\u8853\uff0c\u60a8\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u670d\u52d9\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u958b\u767c\u548c\u6e2c\u8a66\u63d0\u4f9b\u7a69\u5b9a\u7684\u74b0\u5883\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b66\u4e60\u5982\u4f55\u5728 OceanBase \u4e2d\u4f7f\u7528 UNION \u8bed\u53e5\uff0c\u5408\u5e76\u67e5\u8be2\u7ed3\u679c\uff0c\u63d0\u5347\u6570\u636e\u5904\u7406\u6548\u7387\uff0c\u638c\u63e1\u5b9e\u7528\u6280\u5de7\u4e0e\u6700\u4f73\u5b9e\u8df5\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-160281","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160281","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=160281"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160281\/revisions"}],"predecessor-version":[{"id":160282,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160281\/revisions\/160282"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=160281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=160281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=160281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}