Insert into select 和 select into from

来源:互联网 发布:泰妍和西卡关系知乎 编辑:程序博客网 时间:2024/03/29 00:08

     1.INSERT INTO SELECT语句

      语句形式为:Insert into target_table (field1,field2,...) select value1,value2,... from source_table

      要求目标表target_table 必须存在,由于目标表Table2已经存在,所以我们除了插入源表source_table的字段外,还可以插入常量。

 

      2.SELECT INTO FROM语句

      语句形式为:SELECT vale1, value2 into target_table from source_table

      要求目标表target_table 不存在,因为在插入时会自动创建表target_table ,并将source_table中指定字段数据复制到target_table 中。

 

 

    MySQL不支持Select Into语句直接备份表结构和数据:

    执行:SELECT * INTO sms_log_216 FROM sms_log WHERE couponid LIKE '-216%'

    出错:

            Error Code : 1327
            Undeclared variable: sms_log_216

   解决办法:CREATE TABLE sms_log_216 (SELECT * FROM sms_log WHERE couponid LIKE '-216%' )

原创粉丝点击