Link to home
Start Free TrialLog in
Avatar of MarkApplegate
MarkApplegate

asked on

Comparing two postgres tables and updating one with differences.

I have two tables in a postgres database
  Table 1
        newacctnumber
        oldaccountnumber
        date
  Table 2
        newacctnumber
        oldaccountnumber
        done
Table 2 contains all the newacctnumber/oldaccountnumber entrees that exist in table 1.  Some other steps are take and then the done is set to Y.

Table 1 is automatically generated each week, resulting in additional entreens being added.

What I need to do is update table 2 with new entrees that are in Table 1 but not already in Table 2.

This will be done thru an online page (administrators page) with php code.  I have one method but it seems slow to me.

      $query="Select * from table1";
      $db = "poscoid";
      $result = db_query($query, $db);
      
      while ($r=pg_fetch_array($result))
               {
               $newacctnumber = $r["newacctnumber"];
               $oldacctnuber = $r["oldacctnumber"];
   
    $query="Insert into table2(newacctnumber, oldacctnumber) values($newacctnumber,$oldacctnumber) where $newacctnumber not in (select newacctnumber from table2 where done = 'N')";
      
      $db = "poscoid";
      $updateresult = db_query($query, $db);
      
          }
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MarkApplegate
MarkApplegate

ASKER

Thanks for the brain jog hernst42.  I knew there was a way to avoid the php, just couldn't force it out of the grey matter.