我需要选择大多数电影中出现的演员。 我正在使用sakila数据库1.0:
.mwb (MySQL Workbench): https://www.sendspace.com/file/i0z82j
.sql (schema): https://www.sendspace.com/file/vd3hnu
.sql (data): https://www.sendspace.com/file/gbp9ri
我已经拥有的是两个查询
结果:
+------------+-------------+
| first_name | last_name |
+------------+-------------+
| PENELOPE |GUINESS | //4 Films in this examle
| PENELOPE |GUINESS |
| PENELOPE |GUINESS |
| PENELOPE |GUINESS |
| NICK |WAHLBERG | //5 Films in this examle
| NICK |WAHLBERG |
| NICK |WAHLBERG |
| NICK |WAHLBERG |
| NICK |WAHLBERG |
| ED |CHASE | //5 Films in this examle
| ED |CHASE |
| ED |CHASE |
| ED |CHASE |
| ED |CHASE |
| JENNIFER |DAVIS | //4 Films in this examle
| JENNIFER |DAVIS |
| JENNIFER |DAVIS |
| JENNIFER |DAVIS |
| BETTE |NICHOLSON | //3 Films in this examle
| BETTE |NICHOLSON |
| BETTE |NICHOLSON |
| ... |... | //Everyother Actor who appeared in a film
+------------+-------------+
结果:演员1出现在19部电影中。
+----------+-------- +
| actor_id | film_id |
+----------+---------+
| 1 | 1 |
| 1 | 23 |
| 1 | 25 |
| 1 | 106 |
| 1 | 140 |
| 1 | 166 |
| 1 | 277 |
| 1 | 361 |
| 1 | 438 |
| 1 | 499 |
| 1 | 506 |
| 1 | 509 |
| 1 | 605 |
| 1 | 635 |
| 1 | 749 |
| 1 | 832 |
| 1 | 939 |
| 1 | 970 |
| 1 | 980 |
+----------+---------+
但我想要的是: 出现在大多数电影中的演员:
+------------+---------------+--------+
| first_name | last_name | films |
+------------+---------------+--------+
| NICK | WAHLBERG | 5 |
| ED | CHASE | 5 |
| ... | ... | ... |
+------------+----------------+-------+
非常感谢任何帮助。 谢谢!
答案 0 :(得分:0)
无法在您的数据库上运行查询我会这样做:
SELECT count(film_actor.actor_id), actor.first_name, actor.last_name
FROM actor INNER JOIN film_actor ON actor.actor_id = film_actor.actor_id
GROUP BY film_actor.actor_id ;
未测试
答案 1 :(得分:-1)
select concat(a.FIRST_NAME,' ', a.LAST_NAME) as Full_name
from ACTOR a
left join FILM_ACTOR fa on a.ACTOR_ID = fa.ACTOR_ID
group by a.ACTOR_ID, a.FIRST_NAME, a.LAST_NAME
order by count(*) desc
limit 1;