[英]Check rows in first table, compare them with rows from the second table and output a match
I made my story shorter, as requested. 我根据要求缩短了我的故事。
I am trying to create a time table for students. 我正在尝试为学生创建时间表。 I have two tables: 我有两个表:
CREATE TABLE IF NOT EXISTS TIMETABLE
(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
YEAR TEXT NOT NULL,
DATE TEXT,
GROUP TEXT,
BEGIN TEXT,
END TEXT,
ROOM TEXT,
SUBJECT TEXT NOT NULL,
TEACHER TEXT,
IN_DATES INT,
TIMESTAMP TIME)
CREATE TABLE IF NOT EXISTS DATES
(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
SUBJECT TEXT NOT NULL,
TEACHER TEXT NOT NULL,
GROUP TEXT,
DATE TEXT,
TIMESTAMP TEXT
The first table has all classes for all faculties (eg for the first year of studies). 第一个表格包含所有系的所有课程(例如,学习的第一年)。 The second one contains rows with dates of classes that do not take place weekly (that means, eg every 2 weeks or with irregular dates). 第二个包含行的日期不是每周一次的行(这意味着,例如,每两周一次或日期不规则)。
In short: If row has IN_DATES = 0
then we can output it directly. 简而言之:如果row的IN_DATES = 0
则可以直接输出。 If row has IN_DATES = 1
then we need to check if there is a class with the same date and subject in DATES
. 如果row的IN_DATES = 1
那么我们需要检查DATES
是否存在具有相同日期和主题的课程。 If there is a match, we output the whole row from TIMETABLE
. 如果匹配,则从TIMETABLE
输出整个行。 If there is no match, we do not display this row. 如果没有匹配项,则不显示此行。
How to write the SQL query to accomplish this? 如何编写SQL查询来完成此任务?
My English may not be very good as it is not my native language. 我的英语可能不是很好,因为它不是我的母语。
Happy new year ;) 新年快乐 ;)
SELECT *
FROM Timetable t
WHERE in_dates = 0 OR
(
in_dates = 1 AND
EXISTS (
SELECT 1
FROM Dates d
WHERE d.subject = t.subject AND
d.date = t.date
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.