为什么会显示此错误'错误代码:1146。表' sakila.t'不存在'?

时间:2016-06-15 09:39:27

标签: mysql

我想找到使用DERIVED表执行最多电影数量的演员的名字。这个错误是怎么出来的?

我的查询

select actor from
(
    select distinct(actor.first_name) as 'actor',count(film.film_id) as 'film_count' from actor
    join film_actor on film_actor.actor_id=actor.actor_id
    join film on film.film_id=film_actor.film_id
    group by actor
    order by film_count
) as t
where film_count=(select max(film_count) from t);

my query + error message

database sakila

1 个答案:

答案 0 :(得分:0)

    SELECT actor FROM 
    ( 
    SELECT DISTINCT(actor.first_name) AS 'actor',
    COUNT(film.film_id) AS 'film_count' 
    FROM 
    actor 
    JOIN 
    film_actor 
    ON film_actor.actor_id=actor.actor_id 
    JOIN 
    film ON 
    film.film_id=film_actor.film_id 
    GROUP BY actor.first_name 
    ORDER BY film_count 
    ) AS t  order by film_count desc
LIMIT 1

您不能直接在子查询外的where子句中引用子查询中创建的表't'。而是使用上面的查询。我已经按照降序排列了film_count列的结果,并采取了第一行来获得最大数量为film_count的演员