识别系列中的下一集

时间:2019-04-09 15:19:37

标签: sql

我需要用SQL编写查询,以识别用户系列中下一个未观看的剧集。我有一个包含用户历史记录(用户ID,情节ID和节目标题ID)和情节(情节ID(PKID,episochName和playCount)的表),其中历史记录表存储了所有已观看情节的情节ID,而情节表中包含了所有可能的情节。

我曾经希望在两个通过'userID,titleID进行选择的连接之间进行某种联接,但是我要绕圈了。

我总共学习SQL大约5个小时,所以不知道从哪里开始,不胜感激地得到了任何帮助。我知道下面的代码很可怕而且很笨拙(请不要假设任何先验知识!)

我尝试了左联接,但这将返回所有数据,而与用户无关。

Create table Actor(
actorID integer primary key,
first_name text not null,
last_name text not null
);

create table AppearsIn(
actorID integer,
seriesID integer primary key
);

create table Series(
seriesID text,
titleID text,
episodeID text,
releaseDate date
);

create table Episode(
episodeID text primary key,
episodeName text,
playCount integer,
actorID integer
);

create table title(
titleID text primary key,
actorID integer,
titleName text,
genre text,
ageRating integer,
releaseDate date
);

create table history(
userID integer,
titleID text,
episodeID text
);

create table user (
userID integer not null,
fullName text not null,
Email text not null,
dateOfBirth date not null,
subscriptionEndDate date not null
);

insert into Actor (actorID, first_name, last_name)
values (76547, 'tom', 'cruise');

insert into Actor (actorID, first_name, last_name)
values (345, 'val', 'kilner');

insert into AppearsIn (actorID, seriesID)
values (345, 1);

insert into Series (seriesID, titleID, episodeID, releaseDate)
values ('WDS2', 'WalkingDead', 'WDS2E1', '1991-02-02');

insert into Series (seriesID, titleID, episodeID, releaseDate)
values ('WDS2', 'WalkingDead', 'WDS2E2', '1991-02-02');

insert into Series (seriesID, titleID, episodeID, releaseDate)
values ('WDS2', 'WalkingDead', 'WDS2E3', '1991-02-02');

insert into Series (seriesID, titleID, episodeID, releaseDate)
values ('WDS2', 'WalkingDead', 'WDS2E4', '1991-02-02');

insert into Series (seriesID, titleID, episodeID, releaseDate)
values ('WDS2', 'WalkingDead', 'WDS2E5', '1991-02-02');

insert into Series (seriesID, titleID, episodeID, releaseDate)
values ('WDS2', 'WalkingDead', 'WDS2E6', '1991-02-02');

insert into Series (seriesID, titleID, episodeID, releaseDate)
values ('WDS2', 'WalkingDead', 'WDS2E7', '1991-02-02');

insert into Series (seriesID, titleID, episodeID, releaseDate)
values ('WDS2', 'WalkingDead', 'WDS2E8', '1991-02-02');

insert into Episode (episodeID, episodeName, actorID, playCount)
values ('WDS2E1', 'A', 76547, 1);

insert into Episode (episodeID, episodeName, actorID, playCount)
values ('WDS2E2', 'B', 76547, 1);

insert into Episode (episodeID, episodeName, actorID, playCount)
values ('WDS2E3', 'C', 76547, 1);

insert into Episode (episodeID, episodeName, actorID, playCount)
values ('WDS2E4', 'D', 76547, 0);

insert into Episode (episodeID, episodeName, actorID, playCount)
values ('WDS2E5', 'E', 76547, 0);

insert into history (userID, titleID, episodeID)
values (8924, 'Walking Dead','WDS2E1');

insert into history (userID, titleID, episodeID)
values (8924, 'Walking Dead', 'WDS2E2');

insert into history (userID, titleID, episodeID)
values (8924, 'Walking Dead', 'WDS2E3');

insert into user (userID, fullName, Email, dateOfBirth, subscriptionEndDate)
values (8924, 'bill123', 'bill123@warmpost.com', '1970-02-12', '2019-06-05');

insert into title (titleID, actorID, titleName, genre, ageRating, releaseDate)
values ('WalkingDead', 123455, 'The Walking Dead', 'Drama', 15, '2015-01-01');

这是我尝试过的查询:

SELECT * FROM Episode
LEFT JOIN history ON history.episodeID = Episode.EpisodeID
Where playcount <1
limit 1;

1 个答案:

答案 0 :(得分:0)

这听起来像是您对联接有问题。 Here's关于如何进行工作的页面。在该页面上:

(INNER)JOIN :返回两个表中具有匹配值的记录

左(外)联接:从左表返回所有记录,并从右表返回匹配的记录

右(外)联接:从右表返回所有记录,并从左表返回匹配的记录

完全(外部)联接:在左表或右表匹配时返回所有记录

因此,如果只希望用户看过的电影,则可以尝试用RIGHT JOIN替换LEFT JOIN,或更改指定表的顺序。

我目前无法自己对此进行测试,但希望这就是您要寻找的。

SELECT * FROM Episode
RIGHT JOIN history ON history.episodeID = Episode.EpisodeID
WHERE playcount < 1
LIMIT 1;