title: delete records from table using another table?
body: <p>note: to the editors: please edit the title if have a better one :)</p>
  
  <p>my question is:</p>
  
  <p>I have two tables in my database</p>
  
  <pre><code>     -----------
       | table1   |
       |----------|
       | id       |
       |text      |
       ===========
  
  
       -----------
       | table2   |
       |----------|
       | id       |
       |text      |
       ===========
  </code></pre>
  
  <p>table1 is 600,000 records</p>
  
  <p>table2 is 5,000,000 records !!:)</p>
  
  <p>what is the best way to delete all the records in table2 that are not in table1</p>
  
  <p>I main by the way -the fastest way because I don't want to wait 4 hours to complete the process</p>
  
  <p>do you have something better than the following code:</p>
  
  <pre><code>&lt;?PHP
     $sql = "select text from table2";
     $result = mysql_query($sql) or die(mysql_error());
     while($row = mysql_fetch_array($result)){
          $text = $row["text"];
          $sql2 = "select id from table1 where text = '$text'";
          $query2 = mysql_query($sql2) or die(mysql_error());
          $result2 = mysql_num_rows($query2);
          if($result2==0){
               $sql3 = "delete from table2 where text = '$text'";
               $query3 = mysql_query($sql3) or die(mysql_error());
          }
     }
  ?&gt;
  </code></pre>
  
  <p>Thanks</p>