sqlite 对于两个评论者都给同一部电影评分的所有评论者对,返回两个评论者的姓名

kupeojn6  于 2022-11-14  发布在  SQLite
关注(0)|答案(9)|浏览(189)

斯坦福大学自定进度的SQL问题课程:
对于所有审阅者对同一电影进行评分的两个审阅者,返回两个审阅者的姓名。剔除重复项,不要将评审者与自己配对,每对评审者只包含一次。对于每一对,按字母顺序返回该对中的姓名。
该架构:

Movie ( mID, title, year, director )

有一部电影,ID号为MID,片名,上映年份和导演。

Reviewer ( rID, name )

ID号为RID的审阅者有一个特定的名字。

Rating ( rID, mID, stars, ratingDate )

影评人RID在某一天给了这部电影MID星级(1-5)。
我的尝试是:

Select R.Name, R2.Name 
From Reviewer R 
    Join Reviewer R2 on (R.rID = R2.rID)
    Join Rating Rt on (Rt.rID = R2.rID) 
    Join Rating Rt2 on (Rt2.rID = R.rID)
 Where Rt.MID = Rt2.mID and R.rID < r2.rID

我知道我需要有一个包含2个审阅者姓名列和2个电影列的表。我应用的条件是电影必须彼此相等,并且ID不能与问题所说的相同:不要将评论者与他们自己配对,并且每对只能包括一次
我的结果为空(不正确)。我做错了什么?

k10s72fa

k10s72fa1#

您必须将表rating联接两次到表movie,对于每次联接,必须联接表reviewer
然后过滤结果,使审阅者不与自己配对,并通过使用distinctmin()max()函数确保每一对不重复:

select distinct
  min(v1.name, v2.name) reviewer1,
  max(v1.name, v2.name) reviewer2
from movie m
inner join rating r1 on r1.mid = m.mid
inner join rating r2 on r2.mid = m.mid
inner join reviewer v1 on v1.rid = r1.rid
inner join reviewer v2 on v2.rid = r2.rid
where v1.rid <> v2.rid
order by reviewer1, reviewer2
hc8w905p

hc8w905p2#

我会从评级时的自我加入开始,然后引入以下名称:

select distinct rv1.name, rv2.name
from rating r1 join
     rating r2
     on r1.mid = r2.mid join
     reviewer rv1
     on rv1.rid = r1.rid join
     reviewer rv2
     on rv2.rid = r2.rid and rv1.name < rv2.name;

您的查询实际上非常相似。我认为主要的问题是select distinct和按名称排序,而不是id

rwqw0loc

rwqw0loc3#

SELECT DISTINCT MIN(NAME),MAX(NAME) 
FROM (SELECT MID,NAME 
      FROM RATING R,REVIEWER R1 
      WHERE R.RID=R1.RID 
      GROUP BY MID,R.RID 
      ORDER BY MID,NAME)
GROUP BY MID 
ORDER BY MIN(NAME)
fd3cxomn

fd3cxomn4#

正确答案

select distinct rv1.name,rv2.name
from rating r1 
join rating r2 on r1.mid = r2.mid 
join reviewer rv1 on rv1.rID= r1.rID 
join reviewer rv2 on rv2.rID = r2.rID and rv1.name < rv2.name
order by rv1.name,rv2.name;
3hvapo4f

3hvapo4f5#

以下是我的解决方案:

select rev1.*, rev2.*
from Rating rat1
JOIN Rating rat2 on rat2.mID = rat2.mID AND rat1.rID < rat2.rID
JOIN Reviers rev1 ON rev1.rID = rat1.rID
JOIN Reviers rev2 ON rev2.rID = rat2.rID
order by rev1.name, rev2.name

避免使用DISTINCT总是好的。使用ID连接表总是很好的,在此之后,使用一些条件进行排序,比如名称

l2osamch

l2osamch6#

SELECT rev.name, rev2.name
FROM ( SELECT distinct rID, mID
       FROM   RATING  as r )          as t1
INNER JOIN (  SELECT distinct rID, mID
              FROM   RATING  as r )   as t2    On t1.MID = t2.MID
INNER JOIN Reviewer  as rev   ON t1.RID = rev.RID
INNER JOIN Reviewer  as rev2  ON t2.RID = rev2.RID
WHERE t1.RID <> t2.RID
  AND t1.RID < t2.RID 
GROUP by rev.name, rev2.name
ORDER by 1, 2
pdtvr36n

pdtvr36n7#

Select Distinct Min(name) as Mn, Max(name) as Mx 
From Rating Join Reviewer Using(rID)
Group by mID
Order by Mn
34gzjxbg

34gzjxbg8#

select r1.name n1, r2.name n2 from (
    select distinct a1.rid rid1, a2.rid rid2 from rating a1 inner join rating a2 on a1.mid = a2.mid 
    and not a1.rid = a2.rid
)r 
inner join reviewer r1 on r.rid1 = r1.rid
inner join reviewer r2 on r.rid2 = r2.rid
where r1.name < r2.name order by r1.name
uqdfh47h

uqdfh47h9#

select DISTINCT min(name) as rv1, max(name) rv2
from Rating r1, Reviewer
WHERE (SELECT COUNT(*) from Rating r2 WHERE r1.mID = r2.mID) > 1 and r1.rID = Reviewer.rID
GROUP by r1.mID
ORDER by rv1

相关问题