如何在SELECT中使用COUNT别名?

时间:2015-02-24 07:51:39

标签: mysql sql sql-optimization

我收到以下查询的错误:

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;

错误:

Error Code: 1054. Unknown column 'bcount' in 'field list'

我想再次使用select中的count,因为这是一个非常昂贵的查询。我可以做count(mt.tag_id),但这似乎是无法忍受的缓慢。有什么想法来优化或实现计数的别名吗?

表:

medias(显示完整性,查询时不需要)

+----+-------+-------+---------+
| id | attr1 | attr2 | attrEtc |
+----+-------+-------+---------+
| 11 |       |       |         |
| 22 |       |       |         |
+----+-------+-------+---------+

media_tag

+----------+--------+
| media_id | tag_id |
+----------+--------+
|       11 | tag    |
+----------+--------+

tags

+-----+-------+
| id  | count |
+-----+-------+
| tag |     1 |
+-----+-------+

更新:示例数据:http://sqlfiddle.com/#!2/b8085

目标代码的预期响应,或mt.tag_id = tagB以上的查询:

+--------+--------+--------+-----------------+
| tag_id | tcount | bcount | bcount / tcount |
+--------+--------+--------+-----------------+
| tagB   |      2 |      2 | 1               |
| tagC   |      2 |      1 | 0.5             |
| tagA   |      3 |      1 | 0.333           |
+--------+--------+--------+-----------------+

bcount = tag_id和tagB(目标)之间共享media_id的数量

3 个答案:

答案 0 :(得分:2)

而不是alias直接使用Count(mt.tag_id)

SELECT mt.tag_id,
       Count(mt.tag_id) AS bcount,
       Count(mt.tag_id) / 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;

更新:尝试更改此类查询。您可以避开sub-query条款中的where并使用正确的Inner Join

SELECT mt.tag_id,
       Count(mt.tag_id)           AS bcount,
       Count(mt.tag_id) / t.count AS rel,
       t.count                    AS t
FROM   tags.media_tag AS mt
       INNER JOIN tags.tags AS t
               ON mt.tag_id  = t.id
WHERE  mt.tag_id = 'tag'
GROUP  BY mt.tag_id
ORDER  BY rel 

答案 1 :(得分:1)

这是我最终使用的查询。 @NoDisplayName接近但不是一直都在那里。这个SQL实际上在几秒钟内完成运行,并且比原始查询快得多。

SELECT 
    mt.tag_id,
    COUNT(mt.tag_id) / t.count as rel,
    COUNT(mt.tag_id) AS bcount,
    t.count as tcount
FROM
    tags.media_tag AS mt
        INNER JOIN
    (SELECT 
        smt.media_id
    FROM
        tags.media_tag as smt
    WHERE
        tag_id = 'tag') meds ON mt.media_id = meds.media_id
        INNER JOIN
    tags.tags t ON t.id = mt.tag_id
GROUP BY mt.tag_id
ORDER BY rel;

答案 2 :(得分:0)

我认为你在这里犯了错误.... 更正bcount to count(mt.tag_id)

SELECT 
        mt.tag_id,
        count(mt.tag_id) as bcount,
        count(mt.tag_id) / 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;