简体   繁体   English

检查第一个表中的行,将它们与第二个表中的行进行比较并输出匹配项

[英]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.

相关问题 我想基于json-php中输出的第一张表行从第二张表获得输出结果 - I want to get output result from second table based on first table rows output in json-php 如果行匹配,如何比较来自不同表的两行并显示表值? - How to compare two rows from different tables and show the table values if the rows match? 如何检查表中的两行它们的值在其他行中没有重复 - How to check two rows from table that the value of them are not repeated at other rows 将 2 个表行与 Laravel 进行比较 - Compare 2 table rows with Laravel MySQL如何限制仅从第一个表而不从第二个表的JOIN-Query行数? - MySQL How to limit amount of rows from JOIN-Query only from first Table and not the Second Table? 我想在第一张表中获取不在第二张表中的行 - I want to fetch rows in a first table that are not in a second table 计算由id连接的第二个表中的所有行,并将值添加到第一个表的右行 - Count all rows from a second table connected by id and add the value on the right row of the first table 一种有效的方法来计算第二张表中的外键数量并与第一张表中的行一起显示-PHP-MySQL - Efficient way to calculate number of foreign keys in second table and display it with rows from first table - PHP - MySQL 如何在有条件的情况下计算第二张表中的外键数量并在第一张表中的行中显示它-PHP-MySQL - How to calculate number of foreign keys in second table with a condition and display it with rows from first table - PHP - MySQL? 比较两个表,仅在添加其他行时更新第二个表 - compare two tables and only update the second table if there are additional rows added
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM