INSERT INTO … SELECT without detailing all columns

前端 未结 11 1304
不知归路
不知归路 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条回答
  •  旧时难觅i
    2020-12-08 10:42

    Tedious but safe and correct.

    Writing INSERT statements without providing a list of columns leads to code that's hard to debug and, more importantly, very fragile code that will break if the definition of the table is changed.

    If you absolutely can't write the column names out yourself then it's relatively easy to build a tool into your code that will create the comma-separated list for you.

提交回复
热议问题