Note that there are some explanatory texts on larger screens.

plurals
  1. POSELECT something JOIN ON something IN columns of multiple tables
    primarykey
    data
    text
    <p>I'm trying to integrate a bunch of SELECTs all into one. </p> <p>I have multiple tables that look similar to the following:</p> <p><strong>Table: A1</strong></p> <pre><code>+---+----+----+----+----+ |id | I1 | I2 | I3 | I4 | +---+----+----+----+----+ | 1 | 5 | 6 | 1 | 3 | +---+----+----+----+----+ </code></pre> <p><strong>Table: A2</strong></p> <pre><code>+---+----+----+----+ |id | I1 | I2 | I3 | +---+----+----+----+ | 1 | 3 | 10 | 5 | +---+----+----+----+ </code></pre> <p>And 1 table that looks like this:</p> <p><strong>Table: Standards</strong></p> <pre><code>+---+---------+ |id | Name | +---+---------+ | 1 | 'one' | | 2 | 'two' | | 3 | 'three' | | 4 | 'four' | | 5 | 'five' | | 6 | 'six' | | 7 | 'seven' | | 8 | 'eight' | | 9 | 'nine' | |10 | 'ten' | +---+---------+ </code></pre> <p><strong>PHP Code</strong></p> <pre><code>&lt;?php while(/*"A" tables*/){ $noi = // number of columns in this A table $id = // id of this A table $columns = ""; for ($i=1; $i&lt;=$noi; $i++){ $columns = $columns . "A" . $id . ".I" . $i . ", "; } $columns = rtrim($columns, ", "); $sql = "SELECT s.* FROM A" . $id . " INNER JOIN Standards AS s ON s.id IN (" . $columns . ") WHERE A" . $id . ".id=1 ORDER BY s.id ASC"; $result = mysql_query($sql); } ?&gt; </code></pre> <p>I'd like to combine all of these SELECTS into 1. I was thinking I could run a for loop to generate all the names of the tables, but I wasn't sure exactly how to join them with the other JOIN ON ... IN (...) that's in there.</p> <p>The results I want to generate are:</p> <pre><code>+---+---------+ |id | Name | +---+---------+ | 1 | 'one' | | 3 | 'three' | | 5 | 'five' | | 6 | 'six' | |10 | 'ten' | +---+---------+ </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload