title: How to use COUNT alias in SELECT?
body: <p>I get an error for the below query:</p>
  
  <pre><code>SELECT 
      mt.tag_id,
      count(mt.tag_id) as bcount,
      bcount / t.count as rel,
      t.count as t
  FROM
      tags.media_tag as mt,
      tags.tags as t
  WHERE
      mt.media_id in (SELECT 
              mt.media_id
          FROM
              tags.media_tag as mt
          WHERE
              mt.tag_id = 'tag')
  GROUP BY mt.tag_id
  ORDER BY rel
  LIMIT 1000;
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Error Code: 1054. Unknown column 'bcount' in 'field list'
  </code></pre>
  
  <p>I'd like to use the count in the select again as it is quite an expensive query. I can just do <code>count(mt.tag_id)</code> but that seems to be unbearably slow. Any ideas to optimize or achieve alias with the count?</p>
  
  <p>Tables:</p>
  
  <p><code>medias</code> (shown for completeness, not needed in query)</p>
  
  <pre><code>+----+-------+-------+---------+
  | id | attr1 | attr2 | attrEtc |
  +----+-------+-------+---------+
  | 11 |       |       |         |
  | 22 |       |       |         |
  +----+-------+-------+---------+
  </code></pre>
  
  <p><code>media_tag</code></p>
  
  <pre><code>+----------+--------+
  | media_id | tag_id |
  +----------+--------+
  |       11 | tag    |
  +----------+--------+
  </code></pre>
  
  <p><code>tags</code></p>
  
  <pre><code>+-----+-------+
  | id  | count |
  +-----+-------+
  | tag |     1 |
  +-----+-------+
  </code></pre>
  
  <p><strong>UPDATE</strong>: Sample Data: <a href="http://sqlfiddle.com/#!2/b8085" rel="nofollow">http://sqlfiddle.com/#!2/b8085</a></p>
  
  <p>Expected Response where target tag, or in query above <code>mt.tag_id = tagB</code>:</p>
  
  <pre><code>+--------+--------+--------+-----------------+
  | tag_id | tcount | bcount | bcount / tcount |
  +--------+--------+--------+-----------------+
  | tagB   |      2 |      2 | 1               |
  | tagC   |      2 |      1 | 0.5             |
  | tagA   |      3 |      1 | 0.333           |
  +--------+--------+--------+-----------------+
  </code></pre>
  
  <p>bcount = number of shared media_id between tag_id and tagB (target)</p>