title: mysql - pass field value into subquery
body: <p>In this case I am joining by a subquery and want to pass in the deptid to the joined subquery but im getting 'D is not defined' error message.</p>
  
  <pre><code>SELECT * 
      FROM(   
          SELECT D.name AS deptname,D.id AS deptid,WT.sortposition AS deptsortposition 
          FROM departments D JOIN web_taxonomy WT ON (WT.deptid=D.id AND WT.classid=0) 
          WHERE D.web=1
          ORDER BY sortposition
      ) AS D
      LEFT JOIN (
          SELECT C.name AS classname,C. id AS classid,C.department,WT.sortposition AS classsortposition,WT.deptid
          FROM classes C
          JOIN web_taxonomy WT ON (WT.classid=C.id AND WT.subclassid=0 AND WT.deptid=D.deptid) 
          WHERE web=1 ORDER BY classsortposition  
      ) AS C ON (C.department=D.deptid)
  </code></pre>
  
  <p>Any way to pass in the reference similar to what I wrapped in the strong tags above?</p>
  
  <p>EDIT: I made a mistake and left in the working query originally and just added the part that I wanted to function. Essentially I want to minimize the size of the join subquery by already only getting the rows that share the same deptid as was found in the D subquery table.</p>