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: 0mysql> 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

ERROR 1327 (42000): Undeclared variable:相关推荐

  1. 【MySQL -ERROR】MySQL 报错 ERROR 1327 创建函数识别不出数据库中的表(解决方法)

    1.问题背景 今天用MySQL创建含流程控制语句函数,遇到总会识别不出数据库中的courses表,courses是存在数据库中,且可以正常使用的,报错如下: ERROR 1327 (42000): U ...

  2. mysql error 1231_解决ERROR 1231 (42000): Variable 'time_zone' can't

    MySQL根据配置文件会限制Server接受的数据包大小.有时候大的插入和更新会受 max_allowed_packet 参数限制,导致写入或者更新失败.(比方说导入数据库,数据表) mysql 数据 ...

  3. [Err] 1327 - Undeclared variable

    mysql执行select  *  into  tablename  from  table2:Undeclared variable错误 不能创建表导致,将sql语句改为 create  table ...

  4. mysql error 1148_MYSQL入坑第一弹--------ERROR 1148 (42000)ERROR 1290 (HY000)

    这是因为: 服务器端,local_infile默认开启:客户端,local_infile默认关闭,因此用的时候需要打开. On the server side: The local_infile sy ...

  5. mysql的ERROR 1231 (42000)问题原因及解决方法

    报错如下: ERROR 1231 (42000): Variable 'time_zone' can't be set to the value of 'NULL' 常见于使用source执行sql文 ...

  6. php access to undeclared static property,select into from 提示 Undeclared variable.....错误的解决办法...

    在进行数据库表的复制与备份的操作过程中,我们应该都知道有 select into from 和 insert into select 两种方法吧?关于这两种方法的区别和用法,这里就不多说了,有需要的朋 ...

  7. VC解决error C2065: 'timeGetTime' : undeclared identi

    2009-03-28 13:40 在VC的.cpp中加入如下头文件及库的引用. #include <afxinet.h> #include <wininet.h> #inclu ...

  8. linux shell 报错 Syntax error: Bad for loop variable

    在linux下写了一个简单的shell,循环10次. test.sh #!/bin/bash ## ##循环10次 ## for ((i=0; i<10; i++)); do echo Good ...

  9. ERROR 1044 (42000)报错的解决

    [问题背景]用root用户创建用户的时候,报错ERROR 1044 (42000) mysql> grant  select on  *.* to rep@'%' identified by ' ...

最新文章

  1. springboot +element-axios跨域请求
  2. Python3不存在reduce函数
  3. POJ 3617 Best Cow Line(最佳奶牛队伍)
  4. matplotlib plt.plot
  5. C++11 并发指南二(std::thread 详解)
  6. jQuery自动加载更多程序
  7. 不会linux的php难找工作,学linux系统难吗
  8. HTML5离线缓存(Application Cache)
  9. wget 下载百度网盘文件
  10. [uboot 移植]uboot 基础知识
  11. 骗的就是你!揭露买本10大愚蠢表现
  12. Oracle11G数据库重演测试
  13. linux点唱机安装教程,咪哒Minik移动ktv点唱机安装教程(文字版)
  14. 在Tomcat上安装部署SAIKU
  15. Trinity使用流程
  16. mysql怎么设置唯一键_MySQL设置唯一键
  17. 原码、补数、补码以及计算机中为什么用补码存储
  18. 高恪智能流控怎么设置_磊科285G智能QoS与高恪K2P智能流控多用户对比评测(下, 多用户对比测试)...
  19. 学生成绩分等级 --if形式
  20. video JS实现多视频循环播放

热门文章

  1. 湖南大学第十六届程序设计竞赛(重现赛)补题 A
  2. 【别人的原创】上点、离点、内点
  3. 【组合导航】GNSS与惯性及多传感器组合导航附matlab代码
  4. robotframework-ride安装注意点
  5. macbook可以装linux_MacBook Pro 上最适合安装哪个 Linux 发行版?
  6. 阿里云OSS上传 删除
  7. python 指定时间递增_python日期的递增问题
  8. linux 查看samtools版本,Samtools版本更新
  9. 记录一种在C语言中的打桩实现及原理
  10. sap 采购信息记录0净价怎么创建