为每个客户 X 写一个查询,另一个客户 Y 与 X 租了至少一部电影
Posted
技术标签:
【中文标题】为每个客户 X 写一个查询,另一个客户 Y 与 X 租了至少一部电影【英文标题】:Write a query for each customer X, another customer Y who has rented at least one movie in common with X 【发布时间】:2019-10-24 09:05:56 【问题描述】:使用 sakila 数据库,编写一个查询,为每个客户 X 查找另一位客户 Y,该客户至少与 X 共同租借了一部电影。查找所有此类客户对 (X, Y) 并针对每一对,重叠电影的数量。按重叠电影的数量对结果排序
我尝试过使用别名、内部连接和子查询。但是,我认为我的代码存在语法错误。
SELECT o1.customer_id AS CustomerID1,
o2.customer_id AS CustomerID2,
COUNT(*) NoOfOverlappingMovies
FROM( ( (SELECT c.customer_id, f.film_id
FROM customer AS c,
JOIN rental AS r
ON r.customer_id = c.customer_id)
JOIN inventory AS i ON i.inventory_id = r.inventory_id)
JOIN film AS f ON i.film_id = f.film_id
) AS o1
JOIN( ( (SELECT c.customer_id, f.film_id
FROM customer AS c,
JOIN rental AS r
ON r.customer_id = c.customer_id)
JOIN inventory AS i ON i.inventory_id = r.inventory_id)
JOIN film AS f ON i.film_id = f.film_id
) AS o2
ON o2.film_id = o1.film_id AND o2.customer_id < o1.customer_id
GROUP BY o1.customer_id, o2.customer_id
ORDER BY COUNT(*) DESC;
查询应该有 3 列。 CustomerID1、CustomerID2 和 NoOfOverlappingMovies。
【问题讨论】:
那么是 mysql,而不是 SQL Server? 开头段落写得很像家庭作业问题。但是,ORDER BY COUNT(*) DESC
不是有效的 T-SQL。
【参考方案1】:
1) 不要在“FROM”和“JOIN”部分之间使用“,”。
2) 你的括号有点不对劲。我试图在没有表格的情况下尽可能地纠正它们:
SELECT o1.customer_id AS CustomerID1,
o2.customer_id AS CustomerID2,
COUNT(*) NoOfOverlappingMovies
FROM( (SELECT c.customer_id, f.film_id
FROM customer AS c
JOIN rental AS r ON r.customer_id = c.customer_id
JOIN inventory AS i ON i.inventory_id = r.inventory_id
JOIN film AS f ON i.film_id = f.film_id
) AS o1
JOIN (SELECT c.customer_id, f.film_id
FROM customer AS c
JOIN rental AS r ON r.customer_id = c.customer_id
JOIN inventory AS i ON i.inventory_id = r.inventory_id
JOIN film AS f ON i.film_id = f.film_id
) AS o2 ON o2.film_id = o1.film_id AND o2.customer_id < o1.customer_id )
GROUP BY o1.customer_id, o2.customer_id
ORDER BY COUNT(*) DESC;
【讨论】:
【参考方案2】:查询似乎是:
select r1.customer_id, r2.customer_id,
count(distinct r1.film_id) as num_films
from rental r1 join
rental r2
on r1.film_id = r2.film_id and
r1.customer_id < r2.customer_id
group by r1.customer_id, r2.customer_id
order by num_films desc;
此查询似乎不需要其他表。
【讨论】:
以上是关于为每个客户 X 写一个查询,另一个客户 Y 与 X 租了至少一部电影的主要内容,如果未能解决你的问题,请参考以下文章
Apollo 客户端 v3“替换 Y 对象的字段 X 时可能会丢失缓存数据”