以下是教授提出的问题:找出每个城市中播放的电影的最小和最大长度。
以下是我的表格结构:
CREATE TABLE Theatres (
Name varchar2(50) not null,
City varchar2(50) not null,
State varchar2(50) not null,
Zip number not null,
Phone varchar2(50) not null,
PRIMARY KEY (Name)
);
CREATE TABLE Movies (
Title varchar2(100) not null,
Rating NUMBER not null,
Length NUMBER not null,
ReleaseDate date not null,
PRIMARY KEY (Title),
CHECK (Rating BETWEEN 0 AND 10),
CHECK (Length > 0),
CHECK (ReleaseDate > to_date('1/January/1900', 'DD/MONTH/YYYY'))
);
CREATE TABLE ShownAt (
TheatreName varchar2(50) not null,
MovieTitle varchar2(100) not null,
PRIMARY KEY (TheatreName, MovieTitle),
FOREIGN KEY (TheatreName) REFERENCES Theatres(Name),
FOREIGN KEY (MovieTitle) REFERENCES Movies(Title)
);
我尝试了一些不同的查询,但不断遇到问题。这就是我所拥有的:
SELECT MIN(Movies.Length), MAX(Movies.Length), Theatres.Name
FROM Theatres, Movies, ShownAt
WHERE ShownAt.TheatreName = Theatres.Name AND
ShownAt.MovieTitle = Movies.Title AND
Theatres.City IN (SELECT UNIQUE City FROM Theatres);
有人看错了吗?谢谢你的帮助!
答案 0 :(得分:2)
我认为你非常接近。只是缺少GROUP BY
SELECT
MIN(Movies.Length) AS Shortest,
MAX(Movies.Length) AS Longest,
Theatres.City
FROM Theatres
JOIN ShownAt ON ShownAt.TheatreName = Theatres.Name
JOIN Movies ON ShownAt.MovieTitle = Movies.Title
GROUP BY Theatres.City
答案 1 :(得分:1)
我相信这会解决问题:
SELECT T.City, MIN(M.Length) AS MinLength, MAX(M.Length) AS MaxLength
FROM Movies AS M
JOIN ShownAt AS S ON S.MovieTitle = M.Title
JOIN Theatres AS T ON S.TheatreName = T.Name
GROUP BY T.City