{"id":217779,"date":"2023-12-23T00:07:24","date_gmt":"2023-12-22T16:07:24","guid":{"rendered":"https:\/\/www.idc.net\/help\/217779\/"},"modified":"2023-12-23T00:07:24","modified_gmt":"2023-12-22T16:07:24","slug":"mysql%e4%bd%bf%e7%94%a8%e6%89%8b%e5%86%8c%ef%bc%9a%e4%bd%bf%e7%94%a8union%e6%93%8d%e4%bd%9c%e7%ac%a6%e5%90%88%e5%b9%b6%e4%b8%a4%e4%b8%aa%e6%88%96%e5%a4%9a%e4%b8%aaselect%e8%af%ad%e5%8f%a5%e7%9a%84","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/217779\/","title":{"rendered":"MySQL\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528UNION\u64cd\u4f5c\u7b26\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2aSELECT\u8bed\u53e5\u7684\u7ed3\u679c\u96c6"},"content":{"rendered":"<p><title id=\"mysql%e4%bd%bf%e7%94%a8%e6%89%8b%e5%86%8c%ef%bc%9a%e4%bd%bf%e7%94%a8union%e6%93%8d%e4%bd%9c%e7%ac%a6%e5%90%88%e5%b9%b6%e4%b8%a4%e4%b8%aa%e6%88%96%e5%a4%9a%e4%b8%aaselect%e8%af%ad%e5%8f%a5%e7%9a%84-NWhGXcZZeU\">MySQL\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528UNION\u64cd\u4f5c\u7b26\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2aSELECT\u8bed\u53e5\u7684\u7ed3\u679c\u96c6<\/title><\/p>\n<h1 id=\"mysql%e4%bd%bf%e7%94%a8%e6%89%8b%e5%86%8c%ef%bc%9a%e4%bd%bf%e7%94%a8union%e6%93%8d%e4%bd%9c%e7%ac%a6%e5%90%88%e5%b9%b6%e4%b8%a4%e4%b8%aa%e6%88%96%e5%a4%9a%e4%b8%aaselect%e8%af%ad%e5%8f%a5%e7%9a%84-NWhGXcZZeU\">MySQL\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528UNION\u64cd\u4f5c\u7b26\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2aSELECT\u8bed\u53e5\u7684\u7ed3\u679c\u96c6<\/h1>\n<p>\u5728MySQL\u4e2d\uff0cUNION\u64cd\u4f5c\u7b26\u7528\u4e8e\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2aSELECT\u8bed\u53e5\u7684\u7ed3\u679c\u96c6\u3002\u5b83\u5c06\u591a\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u5408\u5e76\u6210\u4e00\u4e2a\u7ed3\u679c\u96c6\uff0c\u5e76\u53bb\u9664\u91cd\u590d\u7684\u884c\u3002<\/p>\n<h2 id=\"%e8%af%ad%e6%b3%95-NWhGXcZZeU\">\u8bed\u6cd5<\/h2>\n<p>UNION\u64cd\u4f5c\u7b26\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>SELECT column1, column2, ...\nFROM table1\nUNION\nSELECT column1, column2, ...\nFROM table2;<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u8bed\u6cd5\u4e2d\uff0c\u6bcf\u4e2aSELECT\u8bed\u53e5\u90fd\u5fc5\u987b\u5177\u6709\u76f8\u540c\u7684\u5217\u6570\u548c\u76f8\u4f3c\u7684\u6570\u636e\u7c7b\u578b\u3002UNION\u64cd\u4f5c\u7b26\u4f1a\u5c06\u4e24\u4e2aSELECT\u8bed\u53e5\u7684\u7ed3\u679c\u96c6\u5408\u5e76\u5728\u4e00\u8d77\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u884c\u7684\u7ed3\u679c\u96c6\u3002<\/p>\n<h2 id=\"%e7%a4%ba%e4%be%8b-NWhGXcZZeU\">\u793a\u4f8b<\/h2>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528UNION\u64cd\u4f5c\u7b26\u5408\u5e76\u4e24\u4e2aSELECT\u8bed\u53e5\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>SELECT name, age\nFROM students\nWHERE age &gt;= 18\nUNION\nSELECT name, age\nFROM employees\nWHERE age &gt;= 18;<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4ece\"students\"\u8868\u548c\"employees\"\u8868\u4e2d\u9009\u62e9\u4e86\u6ee1\u8db3\u5e74\u9f84\u5927\u4e8e\u7b49\u4e8e18\u5c81\u7684\u8bb0\u5f55\uff0c\u5e76\u5c06\u5b83\u4eec\u5408\u5e76\u6210\u4e00\u4e2a\u7ed3\u679c\u96c6\u3002<\/p>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9-NWhGXcZZeU\">\u6ce8\u610f\u4e8b\u9879<\/h2>\n<p>\u5728\u4f7f\u7528UNION\u64cd\u4f5c\u7b26\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u6bcf\u4e2aSELECT\u8bed\u53e5\u7684\u5217\u6570\u548c\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u76f8\u540c\u3002<\/li>\n<li>UNION\u64cd\u4f5c\u7b26\u4f1a\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u7684\u884c\u3002<\/li>\n<li>\u5982\u679c\u60f3\u8981\u4fdd\u7559\u91cd\u590d\u7684\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528UNION ALL\u64cd\u4f5c\u7b26\u3002<\/li>\n<li>UNION\u64cd\u4f5c\u7b26\u9ed8\u8ba4\u6309\u7167\u7b2c\u4e00\u4e2aSELECT\u8bed\u53e5\u7684\u5217\u987a\u5e8f\u8fdb\u884c\u6392\u5e8f\uff0c\u5982\u679c\u60f3\u8981\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\uff0c\u53ef\u4ee5\u4f7f\u7528ORDER BY\u5b50\u53e5\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%80%bb%e7%bb%93-NWhGXcZZeU\">\u603b\u7ed3<\/h2>\n<p>\u4f7f\u7528UNION\u64cd\u4f5c\u7b26\u53ef\u4ee5\u65b9\u4fbf\u5730\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2aSELECT\u8bed\u53e5\u7684\u7ed3\u679c\u96c6\uff0c\u5e76\u53bb\u9664\u91cd\u590d\u7684\u884c\u3002\u5b83\u662fMySQL\u4e2d\u975e\u5e38\u6709\u7528\u7684\u4e00\u4e2a\u529f\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5bfb\u627e\u9999\u6e2f\u670d\u52a1\u5668\uff0c\u540e\u6d6a\u4e91\u662f\u60a8\u7684\u9996\u9009\u3002\u6211\u4eec\u63d0\u4f9b\u9ad8\u6027\u80fd\u7684\u9999\u6e2f\u670d\u52a1\u5668\uff0c\u53ef\u6ee1\u8db3\u60a8\u7684\u5404\u79cd\u9700\u6c42\u3002\u60a8\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6211\u4eec\u7684\u5b98\u7f51\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u9999\u6e2f\u670d\u52a1\u5668\u7684\u4fe1\u606f\uff1a<a href=\"https:\/\/idc.net\">https:\/\/idc.net<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528UNION\u64cd\u4f5c\u7b26\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2aSELECT\u8bed\u53e5\u7684\u7ed3\u679c\u96c6 MySQL\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528UNI [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[197851],"tags":[],"class_list":["post-217779","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/217779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=217779"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/217779\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=217779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=217779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=217779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}