如何组合这些表以获得正确的输出?
How can I combine these tables to get the correct output?
我开始学习 sql 查询并尝试找出一些更复杂的查询(对我来说)。
因为我有这些表和模式:
客户(CustomerID、姓名、地址、年龄、余额)
导演(导演 ID、导演姓名、获奖者)
电影(MovieID、标题、DirectorID、评分、productionStudio、格式、ProductionYear)
DirectorID FK references director
已租借(CustomerID、movieID、Pickupdate、returnDate)
CustomerID FK references Customer
movieID FK references Movies
在电影架构中
format could be ‘VHS’, ‘DVD’, ‘Blue Ray’.
rating in movies could have values such as ‘PG’, ‘PG13’,’ R’… etc
ProductionStudio could have values such as ‘Universal Studio’, ‘Disney’ …etc.
在 Director 架构中
awardWining has a value of 1 if the director won an award otherwise it will be 0.
我正在想办法加入他们,以便找出谁租了超过 3 个标题?
您可以将聚合与 group by 和 having 子句一起使用
select c.CustomerID, c.name,count(title)
from Customer c inner join Rented r
on c.CustomerID=r.CustomerID
inner join Movies m
on r.movieID=m.movieID
group by c.CustomerID, c.name
having count(title)>=3
使用聚合和连接
SELECT r.CustomerId FROM Rented r
join Customer c on c.CustomerId=r.CustomerId
join Movies m on r.movieID= r.movieID
GROUP BY r.CustomerId
HAVING COUNT(DISTINCT m.title) > 3
select c.CustomerId
from Customer c inner join Rented r on r.CustomerId = c.CustomerId
group by c.CustomerId
having count(*) > 3; -- use count(distinct r.MovieId) if repeated rentals is a concern
您似乎不需要 Movies
table 的任何内容,因此没有理由加入。并且假设您这样做了,您通常希望依靠 id 列而不是像 Title
这样的东西,其中两个不同的行实际上可能具有相同的名称。
由于 group by
的工作方式,您经常使用虚拟聚合来 return 额外的详细信息:
select c.CustomerId. min(c.Name) as Name
from Customer c inner join Rented r on r.CustomerId = c.CustomerId
group by c.CustomerId;
另一种方法是避免聚合步骤:
select *
from Customer
where (
select count(*) from Rented r
where r.CustomerId = c.CustomerId
) > 3;
我开始学习 sql 查询并尝试找出一些更复杂的查询(对我来说)。 因为我有这些表和模式:
客户(CustomerID、姓名、地址、年龄、余额)
导演(导演 ID、导演姓名、获奖者)
电影(MovieID、标题、DirectorID、评分、productionStudio、格式、ProductionYear)
DirectorID FK references director
已租借(CustomerID、movieID、Pickupdate、returnDate)
CustomerID FK references Customer
movieID FK references Movies
在电影架构中
format could be ‘VHS’, ‘DVD’, ‘Blue Ray’.
rating in movies could have values such as ‘PG’, ‘PG13’,’ R’… etc
ProductionStudio could have values such as ‘Universal Studio’, ‘Disney’ …etc.
在 Director 架构中
awardWining has a value of 1 if the director won an award otherwise it will be 0.
我正在想办法加入他们,以便找出谁租了超过 3 个标题?
您可以将聚合与 group by 和 having 子句一起使用
select c.CustomerID, c.name,count(title)
from Customer c inner join Rented r
on c.CustomerID=r.CustomerID
inner join Movies m
on r.movieID=m.movieID
group by c.CustomerID, c.name
having count(title)>=3
使用聚合和连接
SELECT r.CustomerId FROM Rented r
join Customer c on c.CustomerId=r.CustomerId
join Movies m on r.movieID= r.movieID
GROUP BY r.CustomerId
HAVING COUNT(DISTINCT m.title) > 3
select c.CustomerId
from Customer c inner join Rented r on r.CustomerId = c.CustomerId
group by c.CustomerId
having count(*) > 3; -- use count(distinct r.MovieId) if repeated rentals is a concern
您似乎不需要 Movies
table 的任何内容,因此没有理由加入。并且假设您这样做了,您通常希望依靠 id 列而不是像 Title
这样的东西,其中两个不同的行实际上可能具有相同的名称。
由于 group by
的工作方式,您经常使用虚拟聚合来 return 额外的详细信息:
select c.CustomerId. min(c.Name) as Name
from Customer c inner join Rented r on r.CustomerId = c.CustomerId
group by c.CustomerId;
另一种方法是避免聚合步骤:
select *
from Customer
where (
select count(*) from Rented r
where r.CustomerId = c.CustomerId
) > 3;