2015-05-27 48 views
0

假設我有三個表與創建子查詢的新實例源碼

電影

mID | title | year | director 

審閱

rID | name 

評級

01間以下的列只
rID | mID | stars | ratingDate 

,我已經給出的說明是:

「對於在同一評論額定同一部電影的兩倍所有的情況下,給了較高的評價,第二次返回評論者的名稱和的標題電影「

我到目前爲止的方法是我已經完成了所有表格的三路合併以獲取所有需要的信息。接下來我要做的就是創建這種三路合併的新實例,並找出評論者和電影在哪裏相等,評分更高。這是我迄今爲止

SELECT * 
FROM (SELECT * 
FROM Movie Mov 
INNER JOIN Rating Rat 
    ON Mov.mID = Rat.mID 
INNER JOIN Reviewer REv 
    ON Rev.rID = Rat.rID) A, A table; 
WHERE ... 

的問題是,我不能夠創建A的新實例,在這裏,我試圖做到這一點這裏A table

如果任何人有任何其他建議,如何更好地處理這個問題更清楚,請讓我知道。

回答

0

所以,像這樣的問題,第一個任務是找到你想要的行。我會做到這一點,像這樣的查詢:

select * 
from Rating as r1 
where exists (
    select 1 
    from rating as r2 
    where r1.rid = r2.rid 
    and r1.mid = r2.mid 
    and r1.stars < r2.stars 
    and r1.ratingDate < r2.ratingDate 
) 

然後添加你要獲取部分(就像你在做):

select title, name 
from Rating as r1 
inner join Movie as m on m.mid = r1.mid 
inner join Reviewer as z on z.rid = r1.rid 
where exists (
    select 1 
    from rating as r2 
    where r1.rid = r2.rid 
    and r1.mid = r2.mid 
    and r1.stars < r2.stars 
    and r1.ratingDate < r2.ratingDate 
) 

下面是測試數據顯示,它的工作提琴:http://sqlfiddle.com/#!7/5f0f8/3

0

試試這個:

SELECT v.Name AS Reviewer, m.Name AS Movide 
FROM (SELECT DISTINCT A.rID, A.mID 
    FROM Rating A INNER JOIN Rating B 
    ON A.rID = B.rID AND A.mID = B. mID 
    WHERE A.ratingDate > B.ratingDate 
    AND A.starts > B.starts 
) s 
JOIN Reviewer v ON v.rID =s.rID 
JOIN Movie m ON m.mID = s.mID 

我沒有對SQLite的測試,但是。