我有 4 张桌子:
CREATE TABLE People(
id integer primary key autoincrement,
'First Name' text,
'Last Name' text,
);
CREATE TABLE Courses(
id integer primary key autoincrement,
'Course Name' text,
'Course Refresh Rate' integer
);
CREATE TABLE [Course Enrolment](
'Person ID' integer,
'Course ID' integer,
FOREIGN KEY ('Person ID') REFERENCES People(id),
FOREIGN KEY ('Course ID') REFERENCES Courses(id)
);
CREATE TABLE [Course Participation](
'Person ID' integer,
'Course ID' integer,
'Date Taken' text,
FOREIGN KEY ('Person ID') REFERENCES People(id),
FOREIGN KEY ('Course ID') REFERENCES Courses(id)
);
我正在尝试选择已注册课程但从未参加过该课程(因此在“课程参与表”中没有记录)或参加该课程超过“课程刷新率”几年前的人员。我写了一个选择语句,但它没有按预期工作。
SELECT [First Name],
[Last Name]
FROM people AS p
LEFT JOIN courses AS c
JOIN [course enrolment] AS ce
JOIN [course participation] AS cp
ON p.id = ce.[Person ID]
AND p.id = cp.[Person ID]
AND c.id = ce.[Course ID]
AND c.id = cp.[Course ID]
WHERE EXISTS(SELECT * FROM [Course Enrolment] as ce_2 WHERE ce_2.[Person ID] = p.id and ce_2.[Course ID] = c.id )
AND ([date taken] < date( 'now', '-' || [course refresh rate] || ' year' )
or NOT EXISTS(SELECT * FROM [Course Participation] WHERE cp.[Person ID] = p.id and cp.[Course ID] = c.id ))
;
我做错了什么?