title: php sql count from 3 tables
body: <p>I need to get a count from a table, based on data from 2 other tables.</p>
  
  <p>This is my tables structure:</p>
  
  <pre><code>table1 (id, name)
  table2 (id, a, b, c)
  table3 (id, blah)
  </code></pre>
  
  <p>Can i do it all in one statement? Something like this:</p>
  
  <pre><code>SELECT count(*) from table3 WHERE table2.x=table1.name
  </code></pre>
  
  <p>The hard part is the x column is the name of 'table1.name'. So i dont actually know what x is when im running the statement.</p>
  
  <p>This makes me think i'd have to run a statement to find the name of x before i run this one.</p>
  
  <p>Or... maybe some JOIN?</p>
  
  <p>CURRENT CODE WHICH I USE:</p>
  
  <pre><code>if ($rs[firearm] != "") {
      $sql_result2 = mysql_query("SELECT * FROM db_firearms WHERE name='$rs[firearm]'", $db);
      $rs2         = mysql_fetch_array($sql_result2);
      $sql_result3 = mysql_query("SELECT * FROM items_firearms WHERE player='$id'", $db);
      $rs3         = mysql_fetch_array($sql_result3);
      if ($rs3[$rs2[shortname]] &lt; 1) {
          mysql_query("UPDATE players SET firearm = '' WHERE id ='$id'");
      }
  }
  </code></pre>