title: Select using dynamically generated tablename
body: <p>I've got the following problem. My table (<code>geo_table</code>) structure is as follows:</p>
  
  <pre><code>foreign_table_id | foreign_table_name | some_other_fields...
  </code></pre>
  
  <p><code>foreign_table_name</code> stores names of different tables and <code>foreign_table_id</code> stores ids within these tables. What I want to do is check whether a row in the above mentioned table stores valid reference to some other table. Thus I need to select a row from foreign table which name is based on string stored in <code>foreign_table_name</code>. My code didn't work because it has syntax error but it clearly presents what I wanted to achieve:</p>
  
  <pre><code>SELECT foreign_table_id, foreign_table_name FROM `geo_table` gt
  WHERE (
      SELECT COUNT(*) FROM gt.foreign_table_name AS t
      WHERE gt.foreign_table_id = t.uid
  ) &gt; 0
  </code></pre>