哪些演员与数据中观察到的电影中最多的其他演员合作过?
mysql> show columns from film_actor;
+-------------+----------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+-------------------+-----------------------------+
| actor_id | smallint(5) unsigned | NO | PRI | NULL | |
| film_id | smallint(5) unsigned | NO | PRI | NULL | |
| last_update | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+----------------------+------+-----+-------------------+-----------------------------+
3 rows in set (0.01 sec)
mysql> show columns from actor ;
+-------------+----------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+-------------------+-----------------------------+
| actor_id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| first_name | varchar(45) | NO | | NULL | |
| last_name | varchar(45) | NO | MUL | NULL | |
| last_update | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+----------------------+------+-----+-------------------+-----------------------------+
4 rows in set (0.06 sec)
mysql> show columns from film ;
+----------------------+---------------------------------------------------------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+---------------------------------------------------------------------+------+-----+-------------------+-----------------------------+
| film_id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | MUL | NULL | |
| description | text | YES | | NULL | |
| release_year | year(4) | YES | | NULL | |
| language_id | tinyint(3) unsigned | NO | MUL | NULL | |
| original_language_id | tinyint(3) unsigned | YES | MUL | NULL | |
| rental_duration | tinyint(3) unsigned | NO | | 3 | |
| rental_rate | decimal(4,2) | NO | | 4.99 | |
| length | smallint(5) unsigned | YES | | NULL | |
| replacement_cost | decimal(5,2) | NO | | 19.99 | |
| rating | enum('G','PG','PG-13','R','NC-17') | YES | | G | |
| special_features | set('Trailers','Commentaries','Deleted Scenes','Behind the Scenes') | YES | | NULL | |
| last_update | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+----------------------+---------------------------------------------------------------------+------+-----+-------------------+-----------------------------+
13 rows in set (0.12 sec)
mysql> show columns from film_list ;
+-------------+------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------------------------+------+-----+---------+-------+
| FID | smallint(5) unsigned | YES | | 0 | |
| title | varchar(255) | YES | | NULL | |
| description | text | YES | | NULL | |
| category | varchar(25) | NO | | NULL | |
| price | decimal(4,2) | YES | | 4.99 | |
| length | smallint(5) unsigned | YES | | NULL | |
| rating | enum('G','PG','PG-13','R','NC-17') | YES | | G | |
| actors | text | YES | | NULL | |
+-------------+------------------------------------+------+-----+---------+-------+
8 rows in set (0.57 sec)
mysql> select actors from film_list ;
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| actors |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| PENELOPE GUINESS, CHRISTIAN GABLE, LUCILLE TRACY, SANDRA PECK, JOHNNY CAGE, MENA TEMPLE, WARREN NOLTE, OPRAH KILMER, ROCK DUKAKIS, MARY KEITEL |
| BOB FAWCETT, MINNIE ZELLWEGER, SEAN GUINESS, CHRIS DEPP |
| NICK WAHLBERG, BOB FAWCETT, CAMERON STREEP, RAY JOHANSSON, JULIANNE DENCH |
| JODIE DEGENERES, SCARLETT DAMON, KENNETH PESCI, FAY WINSLET, OPRAH KILMER |
| GARY PHOENIX, DUSTIN TAUTOU, MATTHEW LEIGH, MATTHEW CARREY, THORA TEMPLE |
| KIRSTEN PALTROW, SANDRA KILMER, JAYNE NEESON, WARREN NOLTE, MORGAN WILLIAMS, KENNETH HOFFMAN, REESE WEST |
答案 0 :(得分:1)
select a.*,COUNT(Distinct c.actor_id) as countOfAllOtherActorsInAllHisFilms
from actor a
left join film_actor b ON a.actor_id = b.actor_id
left join film_actor c ON ( b.film_id = c.film_id AND c.actor_id != a.actor_id)
WHERE 1
GROUP BY a.actor_id
ORDER BY countOfAllOtherActorsInAllHisFilms DESC
如果你想过滤掉一组电影:
select a.*,COUNT(Distinct c.actor_id) as countOfAllOtherActorsInAllHisFilms
from actor a
left join film_actor b ON a.actor_id = b.actor_id
left join film_actor c ON ( b.film_id = c.film_id AND c.actor_id != a.actor_id)
left join film d ON b.film_id=d.film_id
WHERE d.rating='G'
GROUP BY a.actor_id
ORDER BY countOfAllOtherActorsInAllHisFilms DESC
编辑:我为计数功能添加了关键字distinct
。没有它,如果一个演员一起出现在两部电影中,他们将被计算两次。