title: Left join with additional variable check
body: <p>I have three tables (t1, t2, t3) with different data. I need to add a new table (t4) and add to it a new column for all rows in the first 3 tables. To add new records I need to loop through the each table and LEFT JOIN t4 to make sure the records do not exists already. The only way to associate new records in t4 with other tables is to use 2 columns from each: table name and row id. The problem that I have is that row IDs in those tables are numeric and auto-incremented, i.e. not unique.</p>
  <p>My t4 structure is like this:</p>
  <pre><code>id   |  tbl (t1, t2 or t3)  |  row_id (from t1-t3)  | new_val
  </code></pre>
  <p>and I'm trying to select from t1-t3 only the values not present in t4</p>
  <pre><code>SELECT 
     `&quot;.$c1.&quot;` AS id,          
     `&quot;.$c2.&quot;` AS item
  FROM &quot;.$tb.&quot;
  LEFT JOIN t4 ON (&quot;.$tb.&quot;.`&quot;.$c1.&quot;` = t4.row_id) 
  WHERE t4.tbl = &quot;.$tb.&quot;
  </code></pre>
  <p>So if I were to loop through <strong>t1</strong> and store my data in <strong>t4</strong> the results would look like:</p>
  <pre><code>Table t4
  
  ----------------------------
  id |  tbl | row_id | new_val 
  ----------------------------
  1  |  t1  |   1    | val1
  ----------------------------
  2  |  t1  |   2    | val2
  ----------------------------
  3  |  t1  |   3    | val3
  </code></pre>
  <p>However, if I were to loop through <strong>t2</strong> next it will have to skip rows 1-3, assuming they are already stored, because both have the same row IDs. To avoid this I need to use both row ID and table name but I'm not sure how to integrate this in one query.</p>
  <p>The desired outcome should look like:</p>
  <pre><code>Table t4
  
  ----------------------------
  id |  tbl | row_id | new_val 
  ----------------------------
  1  |  t1  |   1    | val1
  ----------------------------
  2  |  t1  |   2    | val2
  ----------------------------
  3  |  t1  |   3    | val3
  ----------------------------
  4  |  t2  |   1    | blah
  ----------------------------
  5  |  t2  |   2    | blah2
  ----------------------------
  6  |  t2  |   3    | blah3
  ----------------------------
  7  |  t3  |   1    | zzz1
  ----------------------------
  8  |  t3  |   2    | zzz2
  ----------------------------
  9  |  t3  |   3    | zzz3
  </code></pre>