注:我不小心把一個問題的句子在這裏(大規模的道歉對我而言),我已經更新了這個帖子週三3月14日在23:21 PM用正確的問題。如何得到這個特別的「查詢」的結果
我已經花了幾個小時試圖找出這個問題,沒有任何人的幫助,但已經意識到我浪費了太多的生產時間,應該更早地問別人。我在這方面有一個體面的裂縫,並且已經如此接近,但無法得到我需要的最終解決方案。什麼我應該得到的是:
對於在同一評論額定同一部電影兩次, 給了較高的評價,第二次返回評論者的名稱 和電影的標題所有案件。
這是我設法弄到這裏查詢:
SELECT reviewer.name, movie.title, rating.stars
FROM (reviewer JOIN rating ON reviewer.rid = rating.rid)
JOIN movie ON movie.mid = rating.mid
GROUP BY reviewer.name
HAVING COUNT(*) >= 2
ORDER BY reviewer.name DESC
(我有一種感覺是有缺失的,其中來自上面的查詢子句,但我不知道在何處放置它)
(據我瞭解,RIGHT和FULL OUTER JOIN的不SQLite中目前支持的)
而且這裏的表和數據(圖片)...
...和DB代碼...
/* Delete the tables if they already exist */
drop table if exists Movie;
drop table if exists Reviewer;
drop table if exists Rating;
/* Create the schema for our tables */
create table Movie(mID int, title text, year int, director text);
create table Reviewer(rID int, name text);
create table Rating(rID int, mID int, stars int, ratingDate date);
/* Populate the tables with our data */
insert into Movie values(101, 'Gone with the Wind', 1939, 'Victor Fleming');
insert into Movie values(102, 'Star Wars', 1977, 'George Lucas');
insert into Movie values(103, 'The Sound of Music', 1965, 'Robert Wise');
insert into Movie values(104, 'E.T.', 1982, 'Steven Spielberg');
insert into Movie values(105, 'Titanic', 1997, 'James Cameron');
insert into Movie values(106, 'Snow White', 1937, null);
insert into Movie values(107, 'Avatar', 2009, 'James Cameron');
insert into Movie values(108, 'Raiders of the Lost Ark', 1981, 'Steven Spielberg');
insert into Reviewer values(201, 'Sarah Martinez');
insert into Reviewer values(202, 'Daniel Lewis');
insert into Reviewer values(203, 'Brittany Harris');
insert into Reviewer values(204, 'Mike Anderson');
insert into Reviewer values(205, 'Chris Jackson');
insert into Reviewer values(206, 'Elizabeth Thomas');
insert into Reviewer values(207, 'James Cameron');
insert into Reviewer values(208, 'Ashley White');
insert into Rating values(201, 101, 2, '2011-01-22');
insert into Rating values(201, 101, 4, '2011-01-27');
insert into Rating values(202, 106, 4, null);
insert into Rating values(203, 103, 2, '2011-01-20');
insert into Rating values(203, 108, 4, '2011-01-12');
insert into Rating values(203, 108, 2, '2011-01-30');
insert into Rating values(204, 101, 3, '2011-01-09');
insert into Rating values(205, 103, 3, '2011-01-27');
insert into Rating values(205, 104, 2, '2011-01-22');
insert into Rating values(205, 108, 4, null);
insert into Rating values(206, 107, 3, '2011-01-15');
insert into Rating values(206, 106, 5, '2011-01-19');
insert into Rating values(207, 107, 5, '2011-01-20');
insert into Rating values(208, 104, 3, '2011-01-02');
我有這樣的另一個比較類似的問題,但如果我得到一些幫助上這一個我應該能夠將這個模式和技術應用到下一個。
在此先感謝! :)
歡呼的答案,雖然這是另一個問題(我最初意外地發佈了錯誤的問題,並用正確的問題更新了這篇文章)。您的反饋將有助於其他一些問題:) – Rob 2012-03-14 10:29:16
@Rob我編輯了我的答案。 – 2012-03-14 23:40:31