-2

我目前正在参加关于数据库的在线斯坦福课程,如果您能帮我解决这个 sql 问题,我将不胜感激。对不起,我是一个完全的菜鸟。

表电影:

mID | title | year | director

表评级

rID | mID | stars | ratingDate

表审阅者

rID | name

对于所有评论者对,使得两位评论者都对同一部电影进行评分,返回两位评论者的姓名。消除重复,不要将审阅者与他们自己配对,并且每对只包含一次。对于每一对,按字母顺序返回该对中的名称。

4

6 回答 6

2

好的。你实际上问了两个不同的问题。

首先,我将回答您在帖子标题中提出的问题。

这应该有效。

select Movie.title, avg(Rating.stars) as AR from
Rating join Movie on Rating.mID = Movie.mID
group by Movie.title
order by AR desc

如果你没有在连接中指定“on”子句,像这样,

select Movie.title, avg(Rating.stars) as AR from
Rating join Movie
group by Movie.title
order by AR desc

那么由于加入的全叉积,所有电影将具有相同的评级,即全球平均水平。您想要对名称进行内部连接,或者在本例中为 id。

其次,这是您提出的另一个问题的答案,关于审稿人对。R1.name < R2.name确保审阅者姓名从左到右排序。并order by R1.name确保这些对将自上而下排序。

select distinct R1.name, R2.name from
Rating Ra1 join Rating Ra2
join Reviewer R1 join Reviewer R2
where Ra1.mID = Ra2.mID and Ra1.rID != Ra2.rID
and R1.name < R2.name
and Ra1.rID = R1.rID and Ra2.rID = R2.rID
order by R1.name
于 2016-06-07T18:06:46.940 回答
0

求每部电影的平均评分:

SELECT 
    [m].[mID],
    [m].[title],
    AVG([r].[stars]) AS [AvgRating]
  FROM [Movie] [m]
  LEFT JOIN [Rating] [r] ON [m].[mID] = [r].[mID]
  GROUP BY 
    [m].[mID],
    [m].[title]

查找每部电影的唯一评论者:

SELECT
    [r].[mID],
    [v].[rID],
    [v].[name]
  FROM [Reviewer] [v]
  INNER JOIN [Rating] [r] ON [v].[rID] = [r].[rID]
  GROUP BY
    [v].[rID],
    [v].[name],
    [r].[mID]

结合:

SELECT 
  [rat].[mID],
  [rat].[title],
  [rev].[rID],
  [rev].[name],
  [rat].[AvgRating]
FROM
(
  SELECT 
    [m].[mID],
    [m].[title],
    AVG([r].[stars]) AS [AvgRating]
  FROM [Movie] [m]
  LEFT JOIN [Rating] [r] ON [m].[mID] = [r].[mID]
  GROUP BY 
    [m].[mID],
    [m].[title]
) AS [rat]
LEFT JOIN
(
  SELECT
    [r].[mID],
    [v].[rID],
    [v].[name]
  FROM [Reviewer] [v]
  INNER JOIN [Rating] [r] ON [v].[rID] = [r].[rID]
  GROUP BY
    [v].[rID],
    [v].[name],
    [r].[mID]
) AS [rev] ON [rat].[mID] = [rev].[mID]
ORDER BY 
  [rat].[mID] ASC,
  [rev].[name] ASC

对于小提琴,看这里

于 2013-01-12T21:54:50.830 回答
0

这会让你开始。

SELECT m.*, ra.*, re.*
FROM Movie m
JOIN Rating ra ON ra.mID = m.mID
JOIN Reviewer re ON re.rID = ra.rID
于 2013-01-12T21:07:33.677 回答
0

也许是这样的:

SELECT rev.rID, rev.name, m.title
FROM [Reviewer] rev 
   JOIN [Rating] rate ON rev.rID = rate.rID
   JOIN [Movie] m ON rate.mID = m.mID
   JOIN (
     SELECT mID
     FROM [Rating] 
     GROUP BY mID
     HAVING COUNT(Distinct rID) > 1
   ) m2 on m.mID = m2.mID
ORDER BY rev.name

- 编辑

如果您还需要获得平均值:

SELECT rev.rID, rev.name, m.title, ar.avgRating
FROM [Reviewer] rev 
   JOIN [Rating] rate ON rev.rID = rate.rID
   JOIN [Movie] m ON rate.mID = m.mID
   JOIN (
     SELECT mID
     FROM [Rating] 
     GROUP BY mID
     HAVING COUNT(Distinct rID) > 1
   ) m2 on m.mID = m2.mID
   JOIN (
     SELECT mID, AVG(stars) as avgRating
     FROM [Rating] 
     GROUP BY mID
   ) ar on m.mID = ar.mID
ORDER BY rev.name

还有SQL 小提琴

于 2013-01-12T21:10:01.490 回答
0

我正在做同样的课程,这是我得到的答案:

SELECT DISTINCT name1, name2
FROM (SELECT R1.name AS name1, R2.name AS name2
FROM (Rating JOIN Reviewer using(rId)) AS R1,
(Rating JOIN Reviewer using(rId)) AS R2
WHERE R1.mId = R2.mId AND R1.rId <> R2.rId) AS Tuple
WHERE name1 < name2;

我不知道为什么 OP 在标题上谈论平均,因为这个问题不需要。

编辑:供进一步参考,以下是数据:https ://class.stanford.edu/c4x/DB/SQL/asset/moviedata.html

于 2015-03-08T19:09:11.880 回答
0
select distinct r2.name, r1.name
from 
(select r1.rid, r1.mid, r2.name
from (select * from reviewer order by name) r2
join (select r1.rid, r1.mid from rating r1 join reviewer r2 on r1.rid = r2.rid order by r2.name) r1
on r2.rid = r1.rid
group by mid
order by r2.name) r1,
(select r1.rid, r1.mid, r2.name
from reviewer r2,
rating r1
where r1.rid = r2.rid)r2
where r1.rid <> r2.rid and r1.mid = r2.mID
order by r2.name, r1.name
于 2014-06-28T05:57:14.333 回答