title: Matching two tables with SQL
body: <p>I have a table named 'companies' where I store general information about the companies in Lithuania that i am listing, therefore the columnns are named name, phonenumber, town, zipcode, etc. </p>
  
  <p>But there's also a column called "region" where I didn't fill in anything, upon entering the data in my MySQL Table. Bummer. So that whole column is empty (i.e. none of the rows carry a value in the 'region' field)</p>
  
  <p>However, I have another table, named "regions" - consisting of two columns: region_name &amp; zipcodes - where all the zipcodes of each region in the country is stored, only seperated by a break in the line. </p>
  
  <p>How do I script an update statement that'll fill in the region_name in  the companies-table that corresponds to the zipcodes of each row (i.e. each company)</p>
  
  <p>Example:</p>
  
  <p>A company has a zipcode of 5000 in the table named 'companies' But there is no region listed. The table called "regions" has the zipcode 5000 in a row where the region_name is "Northwest", where the value is clammed in between other zipcodes, seperated by line breaks. </p>
  
  <p>Here's an image of the 'regions' table:</p>
  
  <p><a href="http://i.snag.gy/MRQje.jpg" rel="nofollow">http://i.snag.gy/MRQje.jpg</a></p>
  
  <p>How do I now script a PHP or SQL statement, that'll fill in "Northwest" for companies that have zipcodes matching that region?</p>
  
  <p>I have made this so far:</p>
  
  <pre><code>$result = mysql_query("SELECT * FROM companies WHERE zipcode LIKE '$something_here'")
  or die(mysql_error()); if(mysql_num_rows($result) &gt; 0){ 
  echo mysql_num_rows($result);
  while ($row = mysql_fetch_assoc($result)){
  } } else { echo "no matches"; }
  } else { echo "ingen matches."; } 
  </code></pre>