gpt4 book ai didi

mysql - 我应该如何修改我的查询

转载 作者:行者123 更新时间:2023-11-30 22:55:13 24 4
gpt4 key购买 nike

我使用以下查询确定了类别中最受欢迎的 Actor (topActor):

SELECT category_id AS cid,
(SELECT actor_id FROM
film_actor JOIN film_category
ON film_actor.film_id = film_category.film_id
WHERE category_id = cid
GROUP BY actor_id
ORDER BY COUNT(*) DESC
LIMIT 0,1) AS topActor
FROM film_actor JOIN film_category
ON film_actor.film_id = film_category.film_id
GROUP BY category_id


+-----+----------+
| cid | topActor |
+-----+----------+
| 1 | 50 |
| 2 | 150 |
| 3 | 17 |
| 4 | 86 |
| 5 | 196 |
| 6 | 48 |
| 7 | 7 |
| 8 | 79 |
| 9 | 164 |
| 10 | 68 |
| 11 | 27 |
| 12 | 108 |
| 13 | 105 |
| 14 | 107 |
| 15 | 83 |
| 16 | 44 |
+-----+----------+
16 rows in set (0.00 sec)

现在我需要从表 中获取相应 Actor 的 first_namelast_name>actor,包含 actor_id、first_namelast_name。我知道如何使用 PL/SQL 或 SAS 来完成它,但我是 MySQL 的新手。请帮忙!谢谢。

最佳答案

据我所知,您想从 actor 的表中获取 fname 和 lname 的值,外键是 actor_id,所以我认为这就可以了。

SELECT category_id AS cid,
(SELECT actor_id FROM
film_actor JOIN film_category
ON film_actor.film_id = film_category.film_id
WHERE category_id = cid
GROUP BY actor_id
ORDER BY COUNT(*) DESC
LIMIT 0,1) AS topActor, actor.fname, actor.lname
FROM film_actor inner JOIN film_category
ON film_actor.film_id = film_category.film_id inner join actor on film_actor.actor_id = actor.id
GROUP BY category_id

我不太明白你的问题是什么,所以如果这个答案是错误的,请告诉我是哪一部分。

关于mysql - 我应该如何修改我的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26709790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com