简体   繁体   中英

SQL List actors same char different films

在此处输入图像描述

List all pairs of actors that have played the same character in different films.

List all pairs of actors that have played the same character in films with the same name.

List all reoccurring characters in the database, that is, characters like James Bond that appear in multiple films with different names.

There are no ideas for these three queries The first one I think we can use subqueries

SELECT *
FROM film
WHERE(
  select COUNT(*) AS R,roles from film
  group by roles 
  HAVING R>=2
)

If I am understanding the questions correctly you should be able to use self joins to answer them

Select a.Principal Star
from film a 
inner join film b on a.Film_No = b.Film_No
where a.Principal Star = b.Principal Star and a.roles = b.roles and a.Film titles <> b.Film titles

Question 1:

select principal_stars,roles,count(distinct film_titles) 
from film
  group by principal_stars,roles 
  HAVING count(distinct film_titles) >1;
  

Question 2:

select 
films
,principal_stars
,roles
,count(*)
from 
film 
group by films,principal_stars,roles
having count(*)>1

Question 3:

select roles
,count(distinct film_titles)
from film
group by roles
having count(*)>1

Not sure if this is the same you're expecting. if not please add some sample output and data as text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM