mySql copy rows into same table with key value changed (not overwriting existing)

后端 未结 3 805
花落未央
花落未央 2021-01-04 04:28

How do I copy a selection of rows from a mySql table and insert with just the key value changed. Can I do a select and insert in same query?

To be precise, what I wa

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-04 05:05

    If your key is auto_increment, and you have a table structure like primary_key | col1 | col2 | col3 then you can do something along the lines of

    INSERT INTO table (
       col1, col2, col3
    ) SELECT col1, col2, col3
    FROM table
    WHERE primary_key IN(1, 2, 45, 54, 774, 4434 /* etc */);
    

提交回复
热议问题