问题:
编写一个SQL查询,检索重复租用的相同内容 客户不止一次租出同一部电影。如果客户租用>同一部电影多次,输出应该只显示一次(顾客姓名,电影的标题)组合
共有3个表格:
我目前有:
select customer.name, movie.title
from ((rental inner join customer on rental.customer_id = customer.id) inner join movie on rental.movie_id = movie.id)
group by customer.name, movie.title
having count(*) > 1;
但这是不正确的 - 任何想法?
答案 0 :(得分:2)
将您的查询重写为:
SELECT c.name, m.title
FROM rental AS r
INNER JOIN customer AS c ON r.customer_id = c.id
INNER JOIN movie AS m ON r.movie_id = m.id
GROUP BY c.name, m.title
HAVING count(*) > 1
;
您的JOIN
语句语法错误
编辑:为了避免命名重复更改GROUP BY
语句:
GROUP BY c.id, m.id