select into from 和 insert into select都是用來複製表的
兩者的主要區別爲:
1)select into from 要求目標表不存在,因爲在插入時會自動創建。
2) insert into select from 要求目標表存在
一、INSERT INTO SELECT語句
1、語句形式爲:
Insert into Table2(field1,field2,...) select value1,value2,... from Table1
2、注意地方:
(1)要求目標表Table2必須存在,並且字段field,field2...也必須存在
(2)注意Table2的主鍵約束,如果Table2有主鍵而且不爲空,則 field1, field2...中必須包括主鍵
(3)注意語法,不要加values,和插入一條數據的sql混了,不要寫成:
Insert into Table2(field1,field2,...) values (select value1,value2,... from Table1)
(4)由於目標表Table2已經存在,所以我們除了插入源表Table1的字段外,還可以插入常量。
二、SELECT INTO FROM語句
語句形式爲:
SELECT vale1, value2 into Table2 from Table1
要求目標表Table2不存在,因爲在插入時會自動創建表Table2,並將Table1中指定字段數據複製到Table2中 。
總結複製表的基本語句(從舊錶複製到新表):
1、create table newTable as select * from oldTabel; 既複製表結構,也複製表內容;
2、create table newTable as select * from oldTable where 1=2; 只複製表結構,不復製表內容;
3、insert into newTable select * form oldTable; 不復製表結構,只複製表內容
或者 select value1,value2 into newTable form oldTable;
ERROR 1327 (42000): Undeclared variable:
select into from 提示 Undeclared variable.....錯誤的解決辦法 && select into from 和 insert into select 的用法和區別
然而今天在使用 SELECT INTO FROM 備份mysql數據表的時候,運行相關 sql 語句的時候卻一直返回 [Err] 1327 - Undeclared variable: ...... 這種錯誤,實在不解,經過查詢相關資料才知道,原來 mysql 數據庫是不支持 SELECT INTO FROM 這種語句的,但是經過研究是可以通過另外一種變通的方法解決這個問題的,下面就來說說解決這個錯誤!
mysql> select user,host into user2 from user;
ERROR 1327 (42000): Undeclared variable: user2
解決方法是:
create table user2 (select * from user);
這種方法會將old_table中所有的內容都拷貝過來,用這種方法需要注意,new_table中沒有了old_table中的primary key,Extra,auto_increment等屬性,需要自己手動加,具體參看後面的修改表即字段屬性.
mysql> create table user2 (select * from user);
Query OK, 6 rows affected (0.18 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> create table user01 (select user,password,host from user);
Query OK, 6 rows affected (0.20 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from user01;
+------+----------+-----------+
| user | password | host |
+------+----------+-----------+
| root | | localhost |
| root | | lmr |
| root | | 127.0.0.1 |
| root | | ::1 |
| | | localhost |
| | | lmr |
+------+----------+-----------+
6 rows in set (0.00 sec)
mysql> desc user01;
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| user | char(16) | NO | | | |
| password | char(41) | NO | | | |
| host | char(60) | NO | | | |
+----------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
注意: SELECT INTO 複製表或表結構的時候,只是得到了一個“外殼”,就像克隆人一樣,只是得到了一個軀體,個人的意識、回憶都不會克隆的。像原表的主鍵、外鍵、約束、觸發 器、索引都不會被複制過來
CREATE TABLE 表名 AS SELECT 語句
1.新表不存在
create table new_table select * from old_talbe;
這種方法會將old_table中所有的內容都拷貝過來,用這種方法需要注意,new_table中沒有了old_table中的primary key,Extra,auto_increment等屬性,需要自己手動加,具體參看後面的修改表即字段屬性.
只複製表結構到新表
# 第一種方法,和上面類似,只是數據記錄爲空,即給一個false條件
create table new_table select * from old_table where 1=2;
# 第二種方法
create table new_table like old_table;
2.新表存在
複製舊錶數據到新表(假設兩個表結構一樣)
insert into new_table select * from old_table;
複製舊錶數據到新表(假設兩個表結構不一樣)
insert into new_table(field1,field2,.....) select field1,field2,field3 from old_table;
複製全部數據
select * into new_table from old_table;
只複製表結構到新表
select * into new_talble from old_table where 1=2;
create table a like b;
create table c_relation as select c.memberId,m.merchantId,memb.phone from c_merchant as m inner join c_customer c on c.userId=m.userId inner join c_member memb on memb.id=c.memberId where memb.status=10;
由上面的使用 CREATE TABLE 表名 AS SELECT 語句可以看出:
1:只會複製表數據和表結構,不會有任何約束。
2:當 where 條件不成立時,只複製表結構,沒有任務數據
EG:
mysql> create table marketing_automation_preview_logs_new like marketing_automation_preview_logs;
mysql> show index from marketing_automation_preview_logs;
mysql> show index from marketing_automation_preview_logs_new;
mysql> insert into marketing_automation_preview_logs_new (select * from marketing_automation_preview_logs where act_time> '1588521600');
mysql> select count(*) from marketing_automation_preview_logs_new;
mysql> select count(*) from marketing_automation_preview_logs where act_time> '1588521600';
mysql> RENAME TABLE marketing_automation_preview_logs TO marketing_automation_preview_logs20200512_back;
mysql> RENAME TABLE marketing_automation_preview_logs_new TO marketing_automation_preview_logs;
mysql> select count(*) from marketing_automation_preview_logs;
總結:
create table marketing_automation_preview_logs_new like marketing_automation_preview_logs;
insert into marketing_automation_preview_logs_new (select * from marketing_automation_preview_logs where act_time> '1588521600');
RENAME TABLE marketing_automation_preview_logs TO marketing_automation_preview_logs20200512_back;
RENAME TABLE marketing_automation_preview_logs_new TO marketing_automation_preview_logs;
show index from marketing_automation_preview_logs;
select count(*) from marketing_automation_preview_logs;
mysql8 參考手冊--SELECT ... INTO語句
SELECT ... INTO形式SELECT 使查詢結果存儲在變量或將其寫入文件:
SELECT ... INTO var_list 選擇列值並將其存儲到變量中。
SELECT ... INTO OUTFILE將選定的行寫入文件。可以指定列和行終止符以生成特定的輸出格式。
SELECT ... INTO DUMPFILE 將單行寫入文件而沒有任何格式。
https://dev.mysql.com/doc/refman/8.0/en/select-into.html