title: Trying to use ID in MySQL SubSubQuery
body: <p>So I'll show you what I'm trying to do and explain my problem, there may be an answer different to the approach I'm trying to take.</p>
  
  <p>The query I'm trying to perform is as follows:</p>
  
  <pre><code>SELECT *
  FROM report_keywords rk
  WHERE rk.report_id = 231
  AND (
      SELECT SUM(t.conv) FROM (
          SELECT conv FROM report_keywords t2 WHERE t2.campaign_id = rk.campaign_id ORDER BY conv DESC LIMIT 10
      ) t
  ) &gt;= 30
  GROUP BY rk.campaign_id
  </code></pre>
  
  <p>The error I get is</p>
  
  <pre><code>Unknown column 'rk.campaign_id' in 'where clause'
  </code></pre>
  
  <p>Obviously this is saying that the table alias rk is not making it to the subsubquery.  What I'm trying to do is get all of the campaigns where the sum of the top 10 conversions is greater than or equal to 30.</p>
  
  <p>The relevant table structure is:</p>
  
  <pre><code>id INT,
  report_id INT,
  campaign_id INT,
  conv INT
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Thanks to Kickstart I was able to do what I wanted. Here's my final query:</p>
  
  <pre><code>SELECT campaign_id, SUM(conv) as sum_conv
  FROM (
      SELECT campaign_id, conv, @Sequence := if(campaign_id = @campaign_id, @Sequence + 1, 1) AS aSequence, @campaign_id := campaign_id
      FROM report_keywords
      CROSS JOIN (SELECT @Sequence :=  0, @campaign_id := 0) Sub1
      WHERE report_id = 231
      ORDER BY campaign_id, conv DESC
  ) t
  WHERE aSequence &lt;= 10
  GROUP BY campaign_id
  HAVING sum_conv &gt;= 30
  </code></pre>