
January 6th, 2004, 04:57 AM
|
|
Contributing User
|
|
Join Date: Jul 2003
Location: Kansas
Posts: 70

Time spent in forums: 12 h 52 m 6 sec
Reputation Power: 6
|
|
|
SQL: Insert...Select?
Hi, i'm looking for a way to duplicate records in a table to the same table, but i need to change fields in the duplicated entries. Does this make sense?
Here's what i got
Code:
$t = mysql_query('SELECT from `table` where `country` = "us"') || die(mysql_error());
while($row = mysql_fetch_assoc($t)) {
extract($row);
$row["another_field"] = "this";
$row["another_field2"] = "that";
// need to it to auto intrement
unset($row["id"]);
mysql_query("INSERT into `table` ... $row ... blah blah") || die(mysql_error());
} // end while
That's works all fine and dandy, but it can sometimes lead to an unreasonable amount of queries. I read into Insert...Select but haven't tested it yet.
Code:
INSERT into `table` SELECT * FROM `table` where `country` = 'us'
Would this work?
Ok, say it did or whatever. Can i get a php function to give me a list of new rows so i could update them? Would mysql_affected_rows() do the job?
Thanks.
EDIT: n/m got the query to work and used select `table` desc limit affected_rows(), rows like a charm. Sometimes ya just gotta be patient 
Last edited by corrupted_wise : January 6th, 2004 at 05:36 PM.
|