简体   繁体   中英

Write a query to find the full name of the actor who has acted in the maximum number of movies

My code is as seen below, but I'm not getting the desired answer, which is to get the full name of the actor whose done the maximum number of movies.

SELECT CONCAT(" ",actor.first_name,actor.last_name) AS Full_name 
FROM actor INNER JOIN film_actor ON actor.actor_id = film_actor.actor_id
GROUP BY actor.actor_id
ORDER BY count(film_actor.actor_id) DESC
LIMIT 2, 1;

在此处输入图像描述

Your query should do what you want, but you need to fix the limit clause. If youw want the one actor with most movies, then you need limit 1 rather than limit 2, 1 . The concat() in the select clause also needs to be fixed - you want the space between the names, not before them.

Another option is to use an aggregate subquery for sorting, which avoids aggregation in the outer query:

select concat(a.first_name, ' ', a.last_name) as full_name
from actor a
order by (select count(*) from film_actor fa where fa.actor_id = a.actor_id) desc
limit 1

Note that neither your query nor this one take in account the possibility of top ties. Your question does not specify that. If that's what needed here, then the strategy would be different - but it seems this is not what you asked for.

If you dont want to use inner query and use only joins, this is the answer:

SELECT concat(FIRST_NAME," ",LAST_NAME) as full_name FROM ACTOR A INNER JOIN FILM_ACTOR FA on A.ACTOR_ID=FA.ACTOR_ID GROUP BY (FA.ACTOR_ID) ORDER BY count(FA.FILM_ID) DESC LIMIT 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM