使用嵌套聚合时,临时表不存在错误

时间:2012-04-02 08:57:13

标签: mysql sql

我正在尝试对我的一个查询使用嵌套聚合:

SELECT t.type, t.avgrent
FROM (  SELECT e.type, AVG(e.rental_rate) AS avgrent
        FROM Equipment e
        GROUP BY e.type) AS t
WHERE t.avgrent IN (SELECT MIN(t.avgrent) FROM t)

我一直收到以下错误:

  

数据库查询错误:表'database.t'不存在

谁能告诉我我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:2)

你不能那样重用T。这样的事情可能是:

SELECT t.type, t.avgrent
FROM (  SELECT e.type, AVG(e.rental_rate) AS avgrent
        FROM Equipment e
        GROUP BY e.type) AS t
WHERE
    t.avgrent IN
(
    SELECT
        MIN(t2.avgrent)
    FROM
    (
        SELECT 
           e.type, 
           AVG(e.rental_rate) AS avgrent
        FROM Equipment e
        GROUP BY e.type
    ) AS t2
)