Search code examples
phpsqlmysqlphp4

PHP, MySQL: Duplicate series of rows, but change 1 column?


I have a table called scheduler_sched which has several columns, including a column called schedule_id.

I need a function where I can pass 2 ids (copy_from_id, copy_to_id) as parameters. And what I need to do is take every row where schedule_id = copy_from_id AND duplicate it but change the copy_from_id to the copy_to_id

So basically I want to to the equivalient of this:

UPDATE scheduler_sched SET schedule_id = 32 WHERE schedule_id = 28

Only I do not want to UPDATE any rows, I want to create duplicates with the new ID's

Does this make sense?

How can I do this?

THANKS!

(By the way schedule_id is not a unique/index field on this table)


Solution

  • Insert into scheduler_sched (column1, column2, column3,schedule_id ) 
    Select column1, column2, column3, 32 from scheduler_sched WHERE schedule_id = 28