基于2列的重复行的SQL查询

发布于 2025-01-27 20:06:10 字数 3487 浏览 4 评论 0 原文

我有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。

I have 3 tables movie, rating and reviewer

movie has 4 columns movieID, title, year, director

rating has 4 columns reviewerID, movieID, stars, ratingDate

reviewer has 2 columns reviewerID, name

How do I query reviewer who rated the same movie more than 1 time and gave it higher rating on the second review.

This is my attempt at query to find rows with duplicate values in 2 columns (meaning the movie has been rated by 1 reviewer more than once), and then somehow I need to query reviewer who gave higher stars on second review.

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;

movie table

movieID Title Year Director
101 Gone with the Wind 1939 Victor Fleming
102 Star Wars 1977 George Lucas
103 The Sound of Music 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 null
203 103 2 2011-01-20
203 108 4 2011-01-12
203 108 2 2011-01-30
204 101 3 2011-01-09
205 103 3 2011-01-27
205 104 2 2011-01-22
205 108 4 null
206 107 3 2011-01-15
206 106 5 2011-01-19
207 107 5 2011-01-20
208 104 3 2011-01-02

reviewer table

reviewerID Name
201 Sarah Martinez
202 Daniel Lewis
203 Brittany Harris
204 Mike Anderson
205 Chris Jackson
206 Elizabeth Thomas
207 James Cameron
208 Ashley White

Expected result

Reviewer Title
Sarah Martinez Gone with the Wind

EDIT: I am using MySQL version 8.0.29

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似狗非友 2025-02-03 20:06:10

使用:

select re.Name,mo.Title
FROM (
      select reviewerID,movieID,ratingDate,Stars
      from rating r 
      where exists (select 1 
              from rating r1 
              where r1.reviewerID=r.reviewerID 
              and r.movieID=r1.movieID 
              and r.ratingDate>r1.ratingDate
              and r.Stars>r1.Stars
       )) as t1
inner join movie mo on t1.movieID=mo.movieID
inner join reviewer re on t1.reviewerID=re.reviewerID

关键部分是存在语句

where exists (select 1 
              from rating r1 
              where r1.reviewerID=r.reviewerID 
              and r.movieID=r1.movieID 
              and r.ratingDate>r1.ratingDate
              and r.Stars>r1.Stars 

,它将仅返回您拥有相同用户多个电影的结果,等级星星比Previos大于Previos。

Use:

select re.Name,mo.Title
FROM (
      select reviewerID,movieID,ratingDate,Stars
      from rating r 
      where exists (select 1 
              from rating r1 
              where r1.reviewerID=r.reviewerID 
              and r.movieID=r1.movieID 
              and r.ratingDate>r1.ratingDate
              and r.Stars>r1.Stars
       )) as t1
inner join movie mo on t1.movieID=mo.movieID
inner join reviewer re on t1.reviewerID=re.reviewerID

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=0c5d850ee3393b054d9af4c4ac241d96

The key part is the EXISTS statement

where exists (select 1 
              from rating r1 
              where r1.reviewerID=r.reviewerID 
              and r.movieID=r1.movieID 
              and r.ratingDate>r1.ratingDate
              and r.Stars>r1.Stars 

which 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

若水般的淡然安静女子 2025-02-03 20:06:10

we don't need to use where in with rating and join with rating

You can try to use Movief

> LEAD 窗口函数要获取下一个启动每个 ReviewerId 您的逻辑要比较旧的开始更新的开始。

SELECT DISTINCT r.Name,m.Title
FROM (
  SELECT reviewerID, 
         movieID,
         Stars,
         LEAD(Stars) OVER(PARTITION BY reviewerID, movieID ORDER BY ratingDate) n_start
  FROM rating 
) t1
INNER JOIN movie m ON t1.movieID = m.movieID
INNER JOIN reviewer r ON r.reviewerID = t1.reviewerID
WHERE Stars < t1.n_start

此示例数据

we don't need to use where in with rating and join with rating

You can try to use lead window function to get the next start each reviewerID and movieID which represent duplicate rating (order by ratingDate)

then compare with your logic to find a newer start greater than older start.

SELECT DISTINCT r.Name,m.Title
FROM (
  SELECT reviewerID, 
         movieID,
         Stars,
         LEAD(Stars) OVER(PARTITION BY reviewerID, movieID ORDER BY ratingDate) n_start
  FROM rating 
) t1
INNER JOIN movie m ON t1.movieID = m.movieID
INNER JOIN reviewer r ON r.reviewerID = t1.reviewerID
WHERE Stars < t1.n_start

This sample data sqlfiddle provide by @ErgestBasha

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文