我的数据库考试真的需要帮助。 我有一个电影数据库,我必须列出每个比Vincent Cassel做更多电影的演员。 我的架构是
CREATE TABLE "ACTOR"
(
"codactor" NUMBER(*,0),
"name" VARCHAR2(50 BYTE),
"surname" VARCHAR2(50 BYTE),
"country" VARCHAR2(50 BYTE)
) SEGMENT CREATION IMMEDIATE
CREATE TABLE "FILM"
(
"CODFILM" NUMBER(*,0),
"title" VARCHAR2(100 BYTE),
"GENERE" VARCHAR2(50 BYTE),
"duration" NUMBER(*,0),
"year" NUMBER(*,0),
"earnings" FLOAT(63),
"valutation" NUMBER(2,1)
) SEGMENT CREATION IMMEDIATE
CREATE TABLE "Plays"
(
"CODFILM" NUMBER(*,0),
"codactor" NUMBER(*,0),
"role" VARCHAR2(300 BYTE),
"income" FLOAT(63)
) SEGMENT CREATION IMMEDIATE
考虑到这一点,我不知道如何计算Vincent Cassel所做的电影数量,并将结果与每个演员完成的电影数量进行比较,只选择比Vincent Cassel制作更多电影的演员。 谢谢大家的帮助。
答案 0 :(得分:0)
这将为您提供一些电影,其中Vincent Cassell主演:
select A.codactor, COUNT(P.codfilm) HowManyMovies
from Plays P join Actor A
on P.codactor = A.codactor
where A.name = 'Vincent Cassel'
group by A.codactor
答案 1 :(得分:0)
This will give you actors WHO HAVE DONE MORE movies than 'Vincent Cassel'
SELECT A.name ,A.surname,count(*) HowManyMovies
FROM Plays P
INNER JOIN Actor A
ON P.codactor = A.codactor
GROUP BY A.name ,A.surname
HAVING count(*) > (
SELECT COUNT(P.codfilm) HowManyMovies
FROM Plays P
INNER JOIN Actor A
ON P.codactor = A.codactor
WHERE A.name = 'Vincent Cassel'
GROUP BY A.codactor
)
答案 2 :(得分:0)
First let's count how many times each actor starred in movies.
Then let's select only these actors who have their play count higher than maximum number for Vincent Cassel (this is to properly handle cases when we have more than one Vincent Cassel in our database). Including coalesce()
let's us take care of a case where no plays are found or an actor is not present in our table.
with act_plays as (
select a."codactor", a."name", a."surname", count(*) as playcount
from "ACTOR" a
inner join "Plays" p on
a."codactor" = p."codactor"
group by a."codactor", a."name", a."surname"
)
select *
from act_plays
where playcount > coalesce((
select max(playcount) -- if more than one Vincent Cassel let's take max val
from act_plays
where "name" = 'Vincent' and "surname" = 'Cassel'
), 0)
We can safely use INNER JOIN
when combining actors and plays because we need actors who starred in more films than Vincent Cassel, thus if it's 0 for Vincent we discard those actors who haven't starred in any film anyways.