title: Connecting tables when querying in MySQL
body: <p>I have 3 tables: <code>tco_articles</code>, <code>tco_module_eostext</code> and <code>tco_articles_modules</code>. My <code>tco_articles</code> has unique <code>id</code> key. One for each article. My <code>tco_module_eostext</code> has unique <code>instance_id</code> that belongs to each article. 
  My <code>tco_articles_modules</code> contains all <code>article_id</code>s, but have 9 times as much <code>instance_id</code>s that are used in other tables.</p>
  
  <p><a href="https://i.stack.imgur.com/jycw3.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/jycw3.jpg" alt="enter image description here"></a></p>
  
  <p>So I can have <code>article_id</code> with <code>instance_id</code> that when you query in the <code>tco_module_eostext</code> will return empty.</p>
  
  <p>I'd like to make a query that will return correct body text for the correct article.</p>
  
  <p>So far I have:</p>
  
  <pre><code>global $wpdb;
  $posts = array();
  
  $ids = $wpdb-&gt;get_results('SELECT DISTINCT instance_id, article_id FROM tco_articles_modules', ARRAY_A);
  </code></pre>
  
  <p>This returns array with all the instances and ids like:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [instance_id] =&gt; 928615
              [article_id] =&gt; 129396
          )
  
      [1] =&gt; Array
          (
              [instance_id] =&gt; 928616
              [article_id] =&gt; 129396
          )
  
      [2] =&gt; Array
          (
              [instance_id] =&gt; 928617
              [article_id] =&gt; 129396
          )
  
      [3] =&gt; Array
          (
              [instance_id] =&gt; 928618
              [article_id] =&gt; 129396
          )
  </code></pre>
  
  <p>You can see that the <code>article_id</code>s are the same but <code>instance_id</code>. When you put </p>
  
  <pre><code>$wpdb-&gt;get_results('SELECT body FROM tco_module_eostext WHERE instance_id=928617 ', ARRAY_A);
  </code></pre>
  
  <p>You may get empty, but for </p>
  
  <pre><code>$wpdb-&gt;get_results('SELECT body FROM tco_module_eostext WHERE instance_id=928618 ', ARRAY_A);
  </code></pre>
  
  <p>You could have some body text.</p>
  
  <p>This is my problem. I need to go through all of them and filter out the not empty ones and assign them correct article. I managed to output the articles</p>
  
  <pre><code>foreach ($ids as $key =&gt; $value) {
      $instance_ID = $value['instance_id'];
      $article_ID = $value['article_id'];
  
      $article_out = $wpdb-&gt;get_results('SELECT * FROM tco_articles WHERE id='.$article_ID.' ', ARRAY_A);
      $posts[$article_ID] = $article_out[0];
  
  }
  </code></pre>
  
  <p>Which returns something like:</p>
  
  <pre><code>Array
  (
      [129396] =&gt; Array
          (
              [id] =&gt; 129396
              [headline] =&gt; Bla bla bla title
              [intro] =&gt; This is cool article intro
              [needs_review] =&gt; 0
              [published] =&gt; 2014-12-16 09:17:00
              [unpublished] =&gt; 
              [hidden] =&gt; 0
              [no_single_page] =&gt; 0
              [permalink] =&gt; link-perma-link-here
              [internal_slug] =&gt; 
              [type_id] =&gt; 3
              [thread_id] =&gt; 0
              [news_id] =&gt; 0
              [header_game_id] =&gt; 0
              [puff_hh_id] =&gt; 0
              [puff_title] =&gt; 
              [hdrcol_id] =&gt; 900
              [review_queued] =&gt; 
              [lock_timeout] =&gt; 0
              [created] =&gt; 2014-12-16 09:17:00
              [updated] =&gt; 2015-01-16 13:51:30
              [created_by] =&gt; 84142
              [updated_by] =&gt; 84142
          )
      ...
  </code></pre>
  
  <p>Now I'd like to append the <code>body</code> text from the <code>tco_module_eostext</code> table.</p>
  
  <p>Is there a query I can use to do this automatically or to do this one at the time and then append to the <code>$posts</code> array?</p>
  
  <p>The foreach method of querying is kinda slow when you have 180000+ posts.</p>
  
  <p>Any help is appreciated.</p>