{"id":114,"date":"2023-11-17T17:29:00","date_gmt":"2023-11-17T09:29:00","guid":{"rendered":"http:\/\/www.cmd137blog.top\/?p=114"},"modified":"2024-04-01T17:31:43","modified_gmt":"2024-04-01T09:31:43","slug":"sql-%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2-%e8%81%94%e5%90%88%e6%9f%a5%e8%af%a2-union-union-all","status":"publish","type":"post","link":"http:\/\/www.cmd137blog.top\/?p=114","title":{"rendered":"SQL-\u591a\u8868\u67e5\u8be2-\u8054\u5408\u67e5\u8be2 union, union all"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u524d\u8a00<\/h1>\n\n\n\n<p>\u5f53\u6211\u4eec\u9700\u8981\u8de8\u591a\u4e2a\u8868\u68c0\u7d22\u4e00\u4e2a\u67e5\u8be2\u7ed3\u679c\u96c6\u65f6\uff0cSQL\u4e2d\u7684\u8054\u5408\u67e5\u8be2\u5c31\u5341\u5206\u6709\u7528\u4e86\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u7814\u7a76\u4ec0\u4e48\u662fSQL\u8054\u5408\u67e5\u8be2\uff0c\u5e76\u63d0\u4f9b\u4f7f\u7528\u573a\u666f\u548c\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e00\u3001\u8054\u5408\u67e5\u8be2\u662f\u4ec0\u4e48\uff1f<\/h1>\n\n\n\n<p>SQL\u8054\u5408\u67e5\u8be2\uff08UNION\uff09\u662f\u7528\u4e8e\u4ece\u4e00\u4e2a\u6216\u591a\u4e2a\u8868\u4e2d\u68c0\u7d22\u6570\u636e\u7684\u9ad8\u7ea7\u7279\u6027\u3002\u901a\u8fc7\u8054\u5408\u67e5\u8be2\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u591a\u4e2aSELECT\u8bed\u53e5\u7684\u7ed3\u679c\u7ec4\u5408\u6210\u4e00\u4e2a\u67e5\u8be2\u7ed3\u679c\u96c6\u5e76\u8fd4\u56de\u7ed9\u7528\u6237\u3002\u4f46\u662f\uff0c\u8bf7\u6ce8\u610f\uff0c<strong><em>\u4e3a\u4e86\u8054\u63a5\uff0c\u6bcf\u4e2a\u67e5\u8be2\u90fd\u5fc5\u987b\u5177\u6709\u76f8\u540c\u7684\u5217\u6570\u548c\u6570\u636e\u7c7b\u578b\u3002<\/em><\/strong><\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u8054\u5408\u67e5\u8be2\u7684\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2, ... columnn FROM table1\nUNION\nSELECT column1, column2, ... columnn FROM table2;<\/code><\/pre>\n\n\n\n<p>\u8be5\u67e5\u8be2\u5c06\u68c0\u7d22\u201c table1\u548ctable2\u201d \u4e2d\u6240\u6709\u5217\u7684\u76f8\u540c\u6570\u636e\u5e76\u7ec4\u5408\u8d77\u6765\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e8c\u3001\u4f7f\u7528\u6b65\u9aa4<\/h1>\n\n\n\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \u5b57\u6bb5\u5217\u8868 FROM \u8868A ...\nUNION &#91;ALL]\nSELECT \u5b57\u6bb5\u5217\u8868 FROM \u8868B ...<\/code><\/pre>\n\n\n\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>UNION ALL \u4f1a\u6709\u91cd\u590d\u7ed3\u679c\uff0cUNION \u4e0d\u4f1a<br>\u8054\u5408\u67e5\u8be2\u6bd4\u4f7f\u7528or\u6548\u7387\u9ad8\uff0c\u4e0d\u4f1a\u4f7f\u7d22\u5f15\u5931\u6548<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e09\u3001\u4f7f\u7528\u573a\u666f\u53ca\u5b9e\u4f8b<\/h1>\n<\/blockquote>\n\n\n\n<p>SQL\u8054\u5408\u67e5\u8be2\u5e38\u7528\u4e8e\u4ee5\u4e0b\u60c5\u5883\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9700\u8981\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\u7ec4\u5408\u8d77\u6765\u6210\u4e3a\u4e00\u4e2a\u7ed3\u679c\u96c6<\/li>\n\n\n\n<li>\u9700\u8981\u5728\u5176\u4ed6\u5217\u4e2d\u68c0\u7d22\u65f6\u5ffd\u7565\u91cd\u590d\u884c\uff0c\u800c\u4e0d\u662f\u4f7f\u7528DISTINCT<\/li>\n\n\n\n<li>\u9700\u8981\u5c06\u7ed3\u679c\u6309\u7279\u5b9a\u5217\u8fdb\u884c\u6392\u5e8f<\/li>\n<\/ul>\n\n\n\n<p><strong>\u5b9e\u4f8b\uff1a<\/strong><br>\u4e3a\u4e86\u8bc1\u660e\u6211\u4eec\u7684\u89c2\u70b9\uff0c\u6211\u4eec\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u5e93\uff0c\u5305\u542b4\u4e2a\u8868\uff1a\u5b66\u751f\uff08Students\uff09\uff0c\u8bfe\u7a0b\uff08Courses\uff09\uff0c\u5b66\u751f\u9009\u8bfe\u8bb0\u5f55\uff08StudentCourses\uff09\uff0c\u5b66\u751f\u4fe1\u606f\uff08StudentInfo\uff09\u3002<\/p>\n\n\n\n<p>Students\u8868\u5305\u542b\u5b66\u751f\u7684\u57fa\u672c\u4fe1\u606f\uff0cCourses\u8868\u5305\u542b\u6240\u6709\u53ef\u7528\u7684\u8bfe\u7a0b\uff0cStudentCourses\u8868\u5c06\u9009\u4fee\u67d0\u95e8\u8bfe\u7a0b\u7684\u5b66\u751f\u4e0e\u8be5\u8bfe\u7a0b\u76f8\u5173\u8054\uff0cStudentInfo\u8868\u5219\u5305\u542b\u5b66\u751f\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u68c0\u7d22\u6709\u5173\u5b66\u751f\u548c\u8bfe\u7a0b\u7684\u4fe1\u606f\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u9700\u8981\u5217\u51fa\u6bcf\u4e2a\u5b66\u751f\u6240\u5b66\u8bfe\u7a0b\u7684\u540d\u79f0\uff0c\u4ee5\u53ca\u4ed6\u4eec\u7684\u5b66\u4e60\u6210\u7ee9\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bSQL\u67e5\u8be2\u8fd9\u4e9b\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT Students.Name, Courses.CourseName, StudentCourses.Score \nFROM Students, Courses, StudentCourses \nWHERE Students.Id = StudentCourses.StudentId AND Courses.Id = StudentCourses.CourseId;<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u67e5\u8be2\u8fd4\u56de\u6211\u4eec\u9700\u8981\u7684\u4fe1\u606f\uff0c\u4f46\u53ea\u5217\u51fa\u4e86\u9009\u4fee\u4e86\u67d0\u95e8\u8bfe\u7a0b\u7684\u5b66\u751f\u7684\u4fe1\u606f\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u67e5\u770b\u5b8c\u6574\u7684\u5b66\u751f\u4fe1\u606f\u548c\u5305\u62ec\u6ca1\u6709\u9009\u4fee\u8bfe\u7a0b\u7684\u5b66\u751f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8054\u5408\u67e5\u8be2\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT Students.Name, Courses.CourseName, StudentCourses.Score \nFROM Students, Courses, StudentCourses \nWHERE Students.Id = StudentCourses.StudentId AND Courses.Id = StudentCourses.CourseId\nUNION\nSELECT Students.Name, 'N\/A', 'N\/A'\nFROM Students \nWHERE Students.Id NOT IN (SELECT StudentId FROM StudentCourses);<\/code><\/pre>\n\n\n\n<p>\u6b64\u67e5\u8be2\u8fd4\u56de\u5b8c\u6574\u7684\u5b66\u751f\u5217\u8868\uff0c\u5982\u679c\u5b66\u751f\u6ca1\u6709\u9009\u4fee\u4efb\u4f55\u8bfe\u7a0b\uff0c\u5219\u5728\u201c\u8bfe\u7a0b\u540d\u79f0\u201d\u548c\u201c\u6210\u7ee9\u201d\u5217\u4e2d\u8fd4\u56deN\/A\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u603b\u7ed3<\/h1>\n\n\n\n<p>\u603b\u4e4b\uff0cSQL\u8054\u5408\u67e5\u8be2\uff08UNION\/UNION ALL\uff09\u5341\u5206\u6709\u7528\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4ece\u591a\u4e2a\u8868\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e30\u5bcc\u591a\u5f69\u7684\u67e5\u8be2\u7ed3\u679c\u96c6\uff0c\u540c\u65f6\u4e5f\u80fd\u63d0\u9ad8SQL\u7684\u7075\u6d3b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u5f53\u6211\u4eec\u9700\u8981\u8de8\u591a\u4e2a\u8868\u68c0\u7d22\u4e00\u4e2a\u67e5\u8be2\u7ed3\u679c\u96c6\u65f6\uff0cSQL\u4e2d\u7684\u8054\u5408\u67e5\u8be2\u5c31\u5341\u5206\u6709\u7528\u4e86\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u7814\u7a76\u4ec0\u4e48\u662fSQL [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[14,15],"class_list":["post-114","post","type-post","status-publish","format-standard","hentry","category-sql","tag-sql","tag-15"],"_links":{"self":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=114"}],"version-history":[{"count":1,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/114\/revisions"}],"predecessor-version":[{"id":115,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/114\/revisions\/115"}],"wp:attachment":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}