2013-03-14 39 views
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 

如果有人能指出我做錯了什麼,我將不勝感激。預先感謝您提供的任何幫助。

+0

也許你應該在內部選擇添加rev1.name eran 2013-03-14 17:42:04

+0

這聽起來合乎邏輯。不過,我不確定該添加哪些內容,但是,請問您可以告訴我嗎? – NinaC 2013-03-14 18:43:55

+0

Btw biziclop,感謝清理我的問題中的代碼:) – NinaC 2013-03-14 18:47:59

回答

1

您可以將<更改爲<>內部select,然後您將獲得每個對兩次,然後在外部選擇中添加名稱約束,如(查找### 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 
+0

現場示例:http://sqlfiddle.com/#! 7/3d026/3 – biziclop 2013-03-15 08:51:35

+0

謝謝eran,這個作品非常漂亮。 – NinaC 2013-03-15 17:23:51