[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1461
  • Last Modified:

How can I update one table field to another table field?

I'm taking data from one table and updating another table.  

MySQL client version: 4.1.22

$query = "SELECT itemnum, description FROM item_test";
$results = mysql_query($query) or die(mysql_error());;

while($row = mysql_fetch_array ($results)) {
    $query = "UPDATE Items SET description = '{$row['description']}'
      WHERE itemnum = '{$row['itemnum']}'";
    mysql_query($query);
}
    mysql_close(); // Close the connection.
    print '<p>Successfully CLOSE connected to MySQL.</p>';

I'm going a little crazy...   It's only updating a few records?  

Could you please review it and see if I did something wrong?  
0
usiff
Asked:
usiff
  • 3
  • 2
1 Solution
 
usiffAuthor Commented:
When I change the sql to this...

      $sql = "UPDATE Items SET description = '".$row['item_test.description']."' WHERE itemnum = '".$row['item_test.itemnum']."'";
   

i'm getting this error

Fatal error: Maximum execution time of 30 seconds exceeded in

Thanks

0
 
Terry WoodsIT GuruCommented:
How about just using one sql?:

update items inner join items_test on items.itemnum = items_test.itemnum
 set items.description = items_test.description
0
 
usiffAuthor Commented:
Hello TerryAtOpus,

First of all it worked... Thanks...

If you don't mind me asking...  
 Does inner join in this sql only look for unique records?  

Thanks

usiff
0
 
Terry WoodsIT GuruCommented:
It targets the rows from items where there exists a row in items_test with the same itemnum.

If the itemnum wasn't unique in either of the tables, then you would probably get odd behaviour, so it's important that it's unique in both.
0
 
usiffAuthor Commented:
TerryAtOpus,

I have 10,000 records in both tables and it only updated 171.  Which is fine, because that's the only ones that I needed.    

Thanks again...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now