{"id":144085,"date":"2024-11-01T05:10:29","date_gmt":"2024-10-31T21:10:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/144085\/"},"modified":"2024-11-01T05:10:30","modified_gmt":"2024-10-31T21:10:30","slug":"sql-%e4%b8%ad-select-into-%e5%92%8c-insert-into-select-%e8%aa%9e%e5%8f%a5%e4%bb%8b%e7%b4%b9","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/144085\/","title":{"rendered":"SQL \u4e2d SELECT INTO \u548c INSERT INTO SELECT \u8a9e\u53e5\u4ecb\u7d39"},"content":{"rendered":"<h1 id=\"sql-%e4%b8%ad-select-into-%e5%92%8c-insert-into-select-%e8%aa%9e%e5%8f%a5%e4%bb%8b%e7%b4%b9-SgSfIMdCaH\">SQL \u4e2d SELECT INTO \u548c INSERT INTO SELECT \u8a9e\u53e5\u4ecb\u7d39<\/h1>\n<p>\u5728 SQL \u4e2d\uff0c\u6578\u64da\u7684\u64cd\u4f5c\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u5c24\u5176\u662f\u5728\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\u3002\u5169\u500b\u5e38\u7528\u7684\u8a9e\u53e5\u662f <code>SELECT INTO<\/code> \u548c <code>INSERT INTO SELECT<\/code>\u3002\u9019\u5169\u8005\u96d6\u7136\u6709\u76f8\u4f3c\u4e4b\u8655\uff0c\u4f46\u5176\u7528\u9014\u548c\u529f\u80fd\u537b\u6709\u6240\u4e0d\u540c\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u9019\u5169\u500b\u8a9e\u53e5\u7684\u7528\u6cd5\u53ca\u5176\u5dee\u7570\u3002<\/p>\n<h2 id=\"select-into-%e8%aa%9e%e5%8f%a5-SgSfIMdCaH\">SELECT INTO \u8a9e\u53e5<\/h2>\n<p><code>SELECT INTO<\/code> \u8a9e\u53e5\u4e3b\u8981\u7528\u65bc\u5f9e\u4e00\u500b\u8868\u4e2d\u9078\u64c7\u6578\u64da\u4e26\u5c07\u5176\u63d2\u5165\u5230\u4e00\u500b\u65b0\u8868\u4e2d\u3002\u9019\u500b\u65b0\u8868\u6703\u5728\u57f7\u884c\u8a9e\u53e5\u6642\u81ea\u52d5\u5275\u5efa\u3002\u9019\u7a2e\u65b9\u6cd5\u7279\u5225\u9069\u5408\u65bc\u9700\u8981\u5feb\u901f\u5275\u5efa\u4e00\u500b\u65b0\u8868\u4e26\u586b\u5145\u6578\u64da\u7684\u60c5\u6cc1\u3002<\/p>\n<h3 id=\"%e8%aa%9e%e6%b3%95-SgSfIMdCaH\">\u8a9e\u6cd5<\/h3>\n<pre><code>SELECT column1, column2, ...\nINTO new_table\nFROM existing_table\nWHERE condition;<\/code><\/pre>\n<h3 id=\"%e7%af%84%e4%be%8b-SgSfIMdCaH\">\u7bc4\u4f8b<\/h3>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba <code>employees<\/code> \u7684\u8868\uff0c\u73fe\u5728\u6211\u5011\u60f3\u8981\u5275\u5efa\u4e00\u500b\u540d\u70ba <code>retired_employees<\/code> \u7684\u65b0\u8868\uff0c\u4e26\u5c07\u6240\u6709\u9000\u4f11\u7684\u54e1\u5de5\u6578\u64da\u8907\u88fd\u904e\u53bb\uff1a<\/p>\n<pre><code>SELECT *\nINTO retired_employees\nFROM employees\nWHERE status = 'retired';<\/code><\/pre>\n<p>\u57f7\u884c\u4e0a\u8ff0\u8a9e\u53e5\u5f8c\uff0c<code>retired_employees<\/code> \u8868\u5c07\u6703\u88ab\u5275\u5efa\uff0c\u4e26\u5305\u542b\u6240\u6709\u7b26\u5408\u689d\u4ef6\u7684\u6578\u64da\u3002<\/p>\n<h2 id=\"insert-into-select-%e8%aa%9e%e5%8f%a5-SgSfIMdCaH\">INSERT INTO SELECT \u8a9e\u53e5<\/h2>\n<p>\u8207 <code>SELECT INTO<\/code> \u4e0d\u540c\uff0c<code>INSERT INTO SELECT<\/code> \u8a9e\u53e5\u7528\u65bc\u5c07\u6578\u64da\u5f9e\u4e00\u500b\u8868\u63d2\u5165\u5230\u5df2\u5b58\u5728\u7684\u53e6\u4e00\u500b\u8868\u4e2d\u3002\u9019\u610f\u5473\u8457\u76ee\u6a19\u8868\u5fc5\u9808\u4e8b\u5148\u5b58\u5728\uff0c\u4e26\u4e14\u5176\u7d50\u69cb\u5fc5\u9808\u8207\u6e90\u8868\u7684\u6578\u64da\u76f8\u5bb9\u3002<\/p>\n<h3 id=\"%e8%aa%9e%e6%b3%95-SgSfIMdCaH\">\u8a9e\u6cd5<\/h3>\n<pre><code>INSERT INTO existing_table (column1, column2, ...)\nSELECT column1, column2, ...\nFROM source_table\nWHERE condition;<\/code><\/pre>\n<h3 id=\"%e7%af%84%e4%be%8b-SgSfIMdCaH\">\u7bc4\u4f8b<\/h3>\n<p>\u5047\u8a2d\u6211\u5011\u5df2\u7d93\u6709\u4e00\u500b\u540d\u70ba <code>active_employees<\/code> \u7684\u8868\uff0c\u4e26\u4e14\u6211\u5011\u60f3\u8981\u5c07\u6240\u6709\u5728\u8077\u7684\u54e1\u5de5\u6578\u64da\u5f9e <code>employees<\/code> \u8868\u4e2d\u63d2\u5165\u5230\u9019\u500b\u8868\u4e2d\uff1a<\/p>\n<pre><code>INSERT INTO active_employees (id, name, status)\nSELECT id, name, status\nFROM employees\nWHERE status = 'active';<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u6240\u6709\u5728\u8077\u7684\u54e1\u5de5\u6578\u64da\u5c07\u88ab\u63d2\u5165\u5230 <code>active_employees<\/code> \u8868\u4e2d\uff0c\u800c\u4e0d\u6703\u5275\u5efa\u65b0\u7684\u8868\u3002<\/p>\n<h2 id=\"select-into-%e8%88%87-insert-into-select-%e7%9a%84%e6%af%94%e8%bc%83-SgSfIMdCaH\">SELECT INTO \u8207 INSERT INTO SELECT \u7684\u6bd4\u8f03<\/h2>\n<ul>\n<li><strong>\u8868\u7684\u5275\u5efa\uff1a<\/strong> <code>SELECT INTO<\/code> \u6703\u5275\u5efa\u4e00\u500b\u65b0\u8868\uff0c\u800c <code>INSERT INTO SELECT<\/code> \u9700\u8981\u76ee\u6a19\u8868\u5df2\u5b58\u5728\u3002<\/li>\n<li><strong>\u6578\u64da\u4f86\u6e90\uff1a<\/strong> \u5169\u8005\u90fd\u53ef\u4ee5\u5f9e\u4e00\u500b\u6216\u591a\u500b\u8868\u4e2d\u9078\u64c7\u6578\u64da\uff0c\u4f46 <code>INSERT INTO SELECT<\/code> \u9700\u8981\u78ba\u4fdd\u6578\u64da\u7d50\u69cb\u76f8\u7b26\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5834\u666f\uff1a<\/strong> <code>SELECT INTO<\/code> \u9069\u5408\u65bc\u9700\u8981\u5feb\u901f\u5275\u5efa\u65b0\u8868\u7684\u60c5\u6cc1\uff0c\u800c <code>INSERT INTO SELECT<\/code> \u66f4\u9069\u5408\u65bc\u5c07\u6578\u64da\u5408\u4f75\u5230\u73fe\u6709\u8868\u4e2d\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-SgSfIMdCaH\">\u7e3d\u7d50<\/h2>\n<p>\u5728 SQL \u4e2d\uff0c<code>SELECT INTO<\/code> \u548c <code>INSERT INTO SELECT<\/code> \u662f\u5169\u500b\u975e\u5e38\u6709\u7528\u7684\u8a9e\u53e5\uff0c\u80fd\u5920\u5e6b\u52a9\u7528\u6236\u6709\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u3002\u4e86\u89e3\u5b83\u5011\u7684\u5340\u5225\u548c\u4f7f\u7528\u5834\u666f\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u505a\u51fa\u66f4\u660e\u667a\u7684\u9078\u64c7\u3002\u7121\u8ad6\u662f\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u638c\u63e1 SQL \u8a9e\u53e5\u7684\u4f7f\u7528\u90fd\u662f\u6578\u64da\u7ba1\u7406\u7684\u91cd\u8981\u57fa\u790e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 SQL \u4e2d\u7684 SELECT INTO \u548c INSERT INTO SELECT \u8a9e\u53e5\uff0c\u638c\u63e1\u6578\u64da\u8907\u88fd\u548c\u8868\u683c\u5275\u5efa\u7684\u6280\u5de7\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-144085","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/144085","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=144085"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/144085\/revisions"}],"predecessor-version":[{"id":144086,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/144085\/revisions\/144086"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=144085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=144085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=144085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}