INSERT INTO … SELECT without detailing all columns

前端 未结 11 1295
不知归路
不知归路 2020-12-08 09:50

How do you insert selected rows from table_source to table_target using SQL in MySQL where:

  • Both tables have the same schema
11条回答
  •  执笔经年
    2020-12-08 10:42

    You can probably do it with prepared statements.

    PREPARE table_target_insert FROM 'INSERT INTO table_target SELECT ? FROM table_source';
    SET @cols:='';
    SELECT @cols:=GROUP_CONCAT(IF(column_name = 'id','NULL',column_name) separator ",") FROM information_schema.columns WHERE table_name='table_source';
    EXECUTE table_target_insert USING @cols;
    

提交回复
热议问题