【问题标题】:Write a query to find the full name of the actor who has acted in the maximum number of movies写一个查询,找到演过最多电影的演员的全名
【发布时间】:2020-11-22 08:43:07
【问题描述】:

我的代码如下所示,但我没有得到想要的答案,即获取完成电影数量最多的演员的全名。

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;

【问题讨论】:

  • 你得到的结果有什么问题?
  • 没有样本测试用例通过。 (0/1) Testcase #1 Status FailedExecution time 3.60s CPU 0s Memory 6MB Description Testcase failed!解决方案的输出与预期输出不匹配。评估日志 解决方案输出 *************************** 1. 行 **************** *********** 佩内洛普·吉尼斯

标签: mysql join count sql-order-by sql-limit


【解决方案1】:

您的查询应该做您想做的事,但您需要修复limit 子句。如果您想要拥有最多电影的演员,那么您需要limit 1 而不是limit 2, 1concat()select 子句中的concat() 也需要修复 - 您希望名称之间有空格,而不是之前

另一种选择是使用聚合子查询进行排序,这样可以避免外部查询中的聚合:

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

请注意,您的查询和本次查询均未考虑排名靠前的可能性。你的问题没有具体说明。如果这就是这里所需要的,那么策略会有所不同——但这似乎不是你所要求的。

【讨论】:

    【解决方案2】:

    如果你不想使用内部查询并且只使用连接,这就是答案:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-21
      • 2020-08-03
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多