INSERT SELECT FROM

如果您的数据在其他表中已经存在,可以通过INSERT SELECT FROM将数据复制到另外一张表。

语法

INSERT INTO table_name 
[( column_name [, …] )]
query;       

参数

  • column_name:列名,如果需要将源表中的部分列数据插入到目标表中,SELECT子句中的列必须与INSERT子句中列的顺序、数据类型一致。

  • query:可以是SELECT FROM TABLE或者SELECT FROM VIEW

示例

  • 以指定列名的方式,从CUSTOMER表中复制某几列数据到NEW_CUSTOMER表中。

    INSERT INTO new_customer (customer_id, customer_name, phone_num)
    SELECT customer_id, customer_name, phone_num FROM customer
    WHERE customer.customer_name = '杨过';                
  • 不指定列名,从CUSTOMER表中复制所有列数据到NEW_CUSTOMER表中。

    INSERT INTO new_customer
    SELECT customer_id,customer_name,phone_num,city_name,sex,id_number,home_address,office_address,age,login_time FROM customer
    WHERE customer.customer_name = '杨过';