Forum Moderators: coopster

Message Too Old, No Replies

PHP/MySQL record copy to another table help

         

NLConsulting

8:56 pm on Jun 28, 2006 (gmt 0)

10+ Year Member



How would one update a record in my_TableA using the information in a record in my_TableB when the tables are not identical?

The straight copy/insert I can do for this situation,

<?PHP
$queryINS="INSERT INTO my_TableA (Var1, Var2, Var3...)
SELECT Var1, Var2, Var3... FROM my_TableB
WHERE ID='$unique_ID'";
?>

however attempting to instead UPDATE the information of an existing record in my_tableA with the information in a record in my_tableB has left me stumped. Can someone help me out?

coopster

2:31 am on Jun 29, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, NLConsulting.

Well you could definitely do the SELECTion first, then pull the result set values into variables and use them to assign new values in a separate UPDATE statement. Or you could do it all in one shot with subqueries:

UPDATE my_TableA SET 
Var1 = (SELECT Var1 FROM my_TableB WHERE ID = 1),
Var2 = (SELECT Var2 FROM my_TableB WHERE ID = 1),
Var3 = (SELECT Var3 FROM my_TableB WHERE ID = 1)
WHERE Var1 = 'val01'
;

NLConsulting

3:04 pm on Jun 29, 2006 (gmt 0)

10+ Year Member



Oh, nice. I didn't even think of that. I might be a little concerned about making so many queries on the database in such a short period of time though, being that there are about 20 variables to pull. I will put your code to the test... thanks!