基于2列的重复行的SQL查询
我有3个表 Movie
,评分
和审阅者
电影
有4列 Movief
标题
,年
,导演
等级
有4列 exighteriD
Movief在
我如何查询评论者
谁对同一电影的评分超过1次,并在第二次评论中给出了更高的评分。
这是我在查询2列中找到具有重复值的行的尝试(这意味着电影已由1个审阅者评为不止一次),然后我需要以某种方式查询审阅者
谁给出了更高的 >星星
第二次评论。
SELECT reviewer.name, movie.title, rating.stars, rating.ratingDate
FROM rating
INNER JOIN reviewer ON reviewer.rID = rating.rID
INNER JOIN movie ON movie.mID = rating.mID
WHERE rating.rID IN (SELECT rating.rID FROM rating GROUP BY rating.rID, rating.mID HAVING COUNT(*) > 1)
ORDER BY reviewer.name, rating.ratingDate;
电影
表
电影ID | 年级 | Victor | 导演 |
---|---|---|---|
101 | 与风车 | 1939年 | Fleming |
102 | Star Wars | 1977 | George Lucas |
103 | 音乐之声 | 1965 | Robert Wise |
104 | E.T. | 1982 | Steven Spielberg |
105 | Titanic | 1997 | James Cameron |
106 | Snow White | 1937 | null |
107 | Avatar | 2009 | James Cameron |
108 | Raiders of the Lost Ark | 1981 | Steven Spielberg |
rating
table
reviewerID | movie ID | Stars | ratingDate |
---|---|---|---|
201 | 101 | 2 | 2011-01-22 |
201 | 101 | 4 | 2011-01-27 |
202 | 106 | 4 | 零 |
203 | 103 | 2 | 2011-01-20 |
203 | 108 | 4 | 2011-01-12 |
203 | 108 | 2 | 2011-01-01-30 |
204 204 | 101 | 3 | 2011-01-09 |
205 205 | 103 | 3 | 2011-01-- 27 |
205 | 104 | 2 | 2011-01-22 |
205 | 108 | 4 | 零 |
206 | 107 | 3 | 2011-01-15 |
206 106 | 106 | 5 | 2011-01-19 |
207 107 | 107 | 5 | 2011-01-20 |
208 | 104 104 104 | 3 | 2011-01-02 |
评论者/code>表
审查 | 名称 |
---|---|
201 | Sarah Martinez |
202 | Daniel Lewis |
203 | Brittany Harris |
204 | Mike Anderson |
205 | Chris Jackson |
206 | Elizabeth Thomas |
207 | James Cameron |
208 | Ashley White |
预期结果
审稿人 | 标题 |
---|---|
Sarah Martinez | 与风 |
编辑:我使用MySQL版本8.0.29。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用:
关键部分是
存在
语句,它将仅返回您拥有相同用户多个电影的结果,等级星星比Previos大于Previos。
Use:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=0c5d850ee3393b054d9af4c4ac241d96
The key part is the
EXISTS
statementwhich will return only the results on which you have the same user more than one movie, the rating Stars are bigger than the previos one based on the ratingDate
we don't need to use
where in
withrating
andjoin
withrating
You can try to use
和
Movief
> LEAD 窗口函数要获取下一个启动每个
ReviewerId 您的逻辑要比较旧的开始更新的开始。
此示例数据
we don't need to use
where in
withrating
andjoin
withrating
You can try to use
lead
window function to get the next start eachreviewerID
andmovieID
which represent duplicate rating (order by ratingDate
)then compare with your logic to find a newer start greater than older start.
This sample data sqlfiddle provide by @ErgestBasha