为什么MySQL告诉我该列不存在?

时间:2013-11-21 10:57:53

标签: mysql subquery

我有3张桌子: 联想(ID,姓名) 种族(ID,姓名) 统计信息(ID,日期,associationId,TYPEID,数量)

我的目标是获得所有关联的列表,以及所有关联的列表,类型列表,数量,先排序,关联总数,然后是每种类型。 为此,我有这个问题:

SELECT association.name AS associationName,
        race.name AS raceName,
        SUM(report.quantity) AS quantity,
        subrequest.totalquantity as totalquantity
    FROM stats report
    JOIN association ON association.id = report.associationId
    JOIN race ON race.id = report.raceId
    JOIN (
        SELECT SUM(report.quantity) AS totalquantity,
            association.id AS subId
        FROM stats report
        JOIN association ON association.id = report.associationId
        WHERE date LIKE "2013-11-%"
        GROUP BY association.id
        ORDER BY totalquantity DESC
    ) subrequest ON subrequest.subId = association.id
    WHERE date LIKE "2013-11-%"
    GROUP BY association.id, race.id
        ORDER BY totalquantity DESC, quantity DESC

它工作得非常好!但现在,我希望每个协会只有两种最大的类型。我试过这个:

set @num := 0, @association := '';
select associationName, raceName, quantity, totalquantity,
@num := if(@association = associationName, @num + 1, 1) as row_number,
        @association := associationName as dummy
FROM (
    SELECT association.name AS associationName,
        race.name AS raceName,
        SUM(report.quantity) AS quantity,
        subrequest.totalquantity as totalquantity
    FROM stats report
    JOIN association ON association.id = report.associationId
    JOIN race ON race.id = report.raceId
    JOIN (
        SELECT SUM(report.quantity) AS totalquantity,
            association.id AS subId
        FROM stats report
        JOIN association ON association.id = report.associationId
        WHERE date LIKE "2013-11-%"
        GROUP BY association.id
        ORDER BY totalquantity DESC
    ) subrequest ON subrequest.subId = association.id
    WHERE date LIKE "2013-11-%"
    GROUP BY association.id, race.id
    ORDER BY totalquantity DESC, quantity DESC
) x
WHERE row_number <= 2

有了这个,我有一个错误:#1054 - 'where子句'中的未知列'row_number'如果我删除WHERE子句,它工作正常,我有预期的结果。我还有一个列row_number,其中包含正确的数字。

我必须这样做才能使它发挥作用:

set @num := 0, @association := '';
select associationName, raceName, quantity, totalquantity
FROM (
    select associationName, raceName, quantity, totalquantity,
    @num := if(@association = associationName, @num + 1, 1) as row_number,
            @association := associationName as dummy
    FROM (
        SELECT association.name AS associationName,
            race.name AS raceName,
            SUM(report.quantity) AS quantity,
            subrequest.totalquantity as totalquantity
        FROM association_race_report report
        JOIN association ON association.id = report.associationId
        JOIN race ON race.id = report.raceId
        JOIN (
            SELECT SUM(report.quantity) AS totalquantity,
                association.id AS subId
            FROM association_race_report report
            JOIN association ON association.id = report.associationId
            WHERE date LIKE "2013-11-%"
            GROUP BY association.id
            ORDER BY totalquantity DESC
        ) subrequest ON subrequest.subId = association.id
        WHERE date LIKE "2013-11-%"
        GROUP BY association.id, race.id
        ORDER BY totalquantity DESC, quantity DESC
    ) x
)y
WHERE row_number <= 2

这很好用!但我不明白为什么我需要添加另一级别的请求才能使WHERE子句起作用?添加无用的查询级别真让我烦恼。有没有办法让它发挥作用?为什么MySQL会引发这个错误?

谢谢!

1 个答案:

答案 0 :(得分:0)

MySQL不喜欢列引用,因为它在查询的select部分中定义 - 它还没有用于连接和where子句。

我看到你发现的解决方案,都是用定义它的表达式替换别名,或者创建一个中间子查询以避免重写整个事物。 (查询优化器将nromally删除中间查询。)