0
我需要選擇最出演電影的演員。 我使用的Sakila數據庫1.0:MySQL Sakila選擇最電影的演員
.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
我已經是兩個查詢
- 其中選擇每一個演員N次(N =出現在電影)。我的查詢: SELECT actor.first_name,actor.last_name FROM actor INNER JOIN film_actor ON actor.actor_id = film_actor.actor_id;
結果:
+------------+-------------+
| 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
+------------+-------------+
- 其選擇每部電影的特定演員出現在 我的查詢: SELECT actor_id,film_id FROM FILM_ACTOR WHERE actor_id = 1;
結果:演員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 |
| ... | ... | ... |
+------------+----------------+-------+
任何幫助,高度讚賞。 謝謝!
見
未測試http://meta.stackoverflow.com/questions/333952/why-should-i - 提供一個mcve爲什麼似乎對我來說是一個非常簡單的sql查詢 – Strawberry
@Strawberry我已編輯的帖子,但我不能給你更多(少),因爲我簡單無法將此數據庫的一部分刪除。我添加了至少可以重建整個數據庫的文件。 – 387t4qy87t43t4387