title: mysql subquery with inner join and limit
body: <p>I have two tables <code>recipes_sa</code> with columns:</p>
  
  <pre><code>recipes_id   recipes_name   recipes_chef
  ----------   ------------   ------------
  </code></pre>
  
  <p>and <code>chefs_sa</code> with columns:</p>
  
  <pre><code>chefs_id   chefs_name
  --------   ----------
  </code></pre>
  
  <p>And I want to get a limited number of recipes with their chef details, using <code>INNER JOIN</code> and <code>LIMIT</code></p>
  
  <p>I made the following function:</p>
  
  <pre><code>function getLimitJoinData($data, $tbls, $ids, $abr, $type, $limit) {
  
              $dataToSelect = implode($data, ',');
  
              $q = "SELECT $dataToSelect";
  
              $q.= " FROM (SELECT * FROM $tbls[0] LIMIT $limit) $abr";
  
  
              for ($i=1; $i &lt; count($tbls); $i++) { 
                  $q .= " ".$type." JOIN ". $tbls[$i] ." ON " . $abr.'.recipes_chef' .' = '. $ids[$i-1][0];   
              }
          }
  </code></pre>
  
  <p>And the query is like this </p>
  
  <pre><code>SELECT chefs_sa.chefs_name,
         recipes_sa.recipes_name 
  FROM (SELECT * FROM recipes_sa LIMIT 8) rec 
  INNER JOIN chefs_sa ON rec.recipes_chef = chefs_sa.chefs_id
  </code></pre>
  
  <p>But when I run the query I had the following warning:</p>
  
  <blockquote>
    <p>Warning: PDO::query(): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'recipes_sa.recipes_name'
    I don't understand why </p>
  </blockquote>
  
  <p>I have the column <code>recipes_name</code> in <code>recipes_sa</code> table, and from what I read that the database runs the “<strong>inner query</strong>” (the one with limit) first, then how the recipes_name column is not found !!</p>