无法在sqlite查询中获得正确的顺序

时间:2013-03-14 17:14:53

标签: sqlite

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

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

1 个答案:

答案 0 :(得分:1)

您可以更改&lt;到&lt;&gt;在内部选择内部,然后你将获得每对两次,然后在外部选择中添加名称的约束,类似于(寻找### 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