1
SELECT DISTINCT
     rev1.name,
     rev2.name

FROM reviewer rev1,
     reviewer rev2,

    (SELECT r1.rid AS r1id,
            r2.rid AS r2id
       FROM rating r1,
            rating r2
      WHERE r1.mID = r2.mID
        AND r1.rid < r2.rID
   ) AS raters

WHERE rev1.rid = raters.r1id
  AND rev2.rid = raters.r2id

ORDER BY rev1.name DESC;

此 sqlite 代码返回以下对:

Brittany Harris   Chris Jackson
Chris Jackson     Ashley White
Daniel Lewis      Elizabeth Thomas
Elizabeth Thomas  James Cameron
Sarah Martinez    Mike Anderson

对中的名称是正确的,但其中一些应该显示相反,也就是说,结果应该是:

Ashley White      Chris Jackson
Brittany Harris   Chris Jackson
Daniel Lewis      Elizabeth Thomas
Elizabeth Thomas  James Cameron
Mike Anderson     Sarah Martinez

如果有人能指出我做错了什么,我将不胜感激。提前感谢您提供的任何帮助。

4

1 回答 1

1

您可以在内部选择中将 < 更改为 <>,然后您将获得每对两次,然后在外部选择中为名称添加约束,例如(查找 ### HERE)。我也认为你想要 ASC 而不是 DESC

SELECT DISTINCT
     rev1.name,
     rev2.name

FROM reviewer rev1,
     reviewer rev2,

    (SELECT r1.rid AS r1id,
            r2.rid AS r2id
       FROM rating r1,
            rating r2
      WHERE r1.mID = r2.mID
        AND r1.rid <> r2.rID  -- HERE - give you each pair twice
   ) AS raters

WHERE rev1.rid = raters.r1id
  AND rev2.rid = raters.r2id
  AND rev1.name < rev2.name  -- HERE - will remove duplicate pairs
  ORDER BY ASC
于 2013-03-15T07:44:30.767 回答