title: How to populate a table's foreign keys from other tables
body: <p>I've got the following tables, of which <code>translation</code> is empty and I'm trying to fill:</p>
  
  <pre><code>translation {
      id
      translated
      language_id
      template_id
  }
  
  language {
      id
      langname
      langcode
  }
  
  template {
      id
      tplname
      source
      domain
      total
  }
  </code></pre>
  
  <p>The source data to fill <code>translation</code> is a temporary table that I've populated from an external CSV file:</p>
  
  <pre><code>tmp_table {
      id
      translated
      langname
      tplname
      source
      domain
  }
  </code></pre>
  
  <p>What I'd like to do is to fill <code>translation</code> with the values from <code>tmp_table</code>. The <code>translated</code> field can be copied directly, but I'm not quite sure how to fetch the right <code>language_id</code> (tmp_table.langname could be used to determine language.id) and <code>template_id</code> (tmp_table.tplname, tmp_table.source, tmp_table.domain together can be used to determine template.id).</p>
  
  <p>It might be a trivial question, but I'm quite new to SQL and not sure what the best query should be to populate the <code>translation</code> table. Any ideas? </p>