PDA

View Full Version : Copying a row and adding a column


zip_000
02-23-2010, 07:19 PM
Hello,

I need to copy a row from one table to another, and the second table needs to have, in addition to the row data, the date that it was moved....and I just can't quite grasp the syntax that I need to do this.

Here's what I've got so far:
INSERT INTO db.table1 Select * from db.table2 WHERE Primary_Key = $Primary_Key

Which would work if I didn't need to add the row. What is the syntax that I need?

Thanks!

angst
02-23-2010, 08:06 PM
try this;



INSERT INTO targetTable (field1, field2) (SELECT field1, field2 FROM sourceTable WHERE condition=check)



also, to get the copy date, just add another field to the table and make it a timestamp.

zip_000
02-23-2010, 08:35 PM
OK, I got it.

Here's what I did:
I split it up into two queries. In the first one I specified which fields I needed:
"INSERT INTO db1.tbl2 (`Created`, `Primary_Key`, [etc]) Select * from db1.tbl1 WHERE Primary_Key = $Primary_Key";

And the second one like this:
"UPDATE db1.tbl2 SET Removed = CURDATE() WHERE Primary_Key = $Primary_Key";


I'm basically deleting info from a primary table, but I don't want to just toss out that data, so I'm putting it in a separate table.

Thanks for the suggestion!:thumbsup: