简体   繁体   English

如何选择其他列中不存在外键的位置,以及如何从未直接链接的表中比较日期

[英]How To Select Where Foreign Key Doesn't Exist in Other Column and Compare Dates From Tables That Aren't Directly Linked

EER-DIAGRAM DATABASE: https://i.stack.imgur.com/5v87y.jpg EER-DIAGRAM数据库: https : //i.stack.imgur.com/5v87y.jpg

So how this works is that I have a php application where a user can give his available dates (see table: availableDates) for a certain course. 因此,这是如何工作的,我有一个php应用程序,用户可以在其中提供特定课程的可用日期(请参见表:availableDates)。 Based on that preference the system will give him course executions information (see table: courseExecutions) that meet his criteria for which he can then sign up to, meaning that the SQL-query must have the following requirements: 根据该首选项,系统将向他提供满足其条件的课程执行信息(请参阅表:courseExecutions),然后可以为其注册,这意味着SQL查询必须具有以下要求:

  • SELECT courseExecution.courseExecutionID, courseExecution.beginDate, courseExecution.endDate, course.title, course.price, co.city WHERE : SELECT courseExecution.courseExecutionID,courseExecution.beginDate,courseExecution.endDate,course.title,course.price,co.city在哪里

  • A person has not yet signed up for this execution, this gets checked in the participant table, if courseExecutionID is not linked to a participantID in the participant table then he has not signed up yet. 一个人尚未注册此执行,这将在参与者表中进行检查,如果courseExecutionID未链接到参与者表中的参与者ID,则他尚未注册。

  • The CourseExecution must not be done, pretty simple just check whether the done column is 0. CourseExecution一定不能完成,很简单,只需检查一下done列是否为0。
  • availableDates.beginDate is smaller (<) than courseExecution.beginDate availableDates.beginDate(<)小于courseExecution.beginDate
  • availableDates.endDate is greater (>) than courseExecution.endDAte availableDates.endDate(>)大于courseExecution.endDAte

I have written a lot of queries but none of them have managed to cover the use case. 我写了很多查询,但是没有一个能解决用例。 A (non-working) example would be: 一个(不起作用的)示例是:

SELECT c.courseExecutionID, c.beginDate, c.endDate, co.title, co.price, co.title
FROM courseExecution c
JOIN course co
ON c.courseID = co.courseID
JOIN availableDates a
ON co.courseID = a.courseID
JOIN person p 
ON a.personID = p.personID
WHERE NOT EXISTS (SELECT personID FROM participant WHERE p.personID = participant.personID)
AND c.done = 0
ORDER BY courseExecutionID

To make it better, try to break your conditions in little pieces: 为了使它更好,请尝试将您的条件分成几部分:

  • SELECT courseExecution.courseExecutionID, courseExecution.beginDate, courseExecution.endDate, course.title, course.price, co.city WHERE: SELECT courseExecution.courseExecutionID,courseExecution.beginDate,courseExecution.endDate,course.title,course.price,co.city在以下位置:

    SELECT c.courseExecutionID, c.beginDate, c.endDate, co.title, co.price, co.title


  • A person has not yet signed up for this execution, this gets checked in the participant table, if courseExecutionID is not linked to a participantID in the participant table then he has not signed up yet. 一个人尚未注册此执行,这将在参与者表中进行检查,如果courseExecutionID未链接到参与者表中的参与者ID,则他尚未注册。

    c.courseExecutionID NOT IN (SELECT courseExecutionID FROM participant)


  • The CourseExecution must not be done, pretty simple just check whether the done column is 0. CourseExecution一定不能完成,很简单,只需检查一下done列是否为0。

    c.done = 0


  • availableDates.beginDate is smaller (<) than courseExecution.beginDate availableDates.beginDate(<)小于courseExecution.beginDate

    a.beginDate < c.beginDate


  • availableDates.endDate is greater (>) than courseExecution.endDAte availableDates.endDate(>)大于courseExecution.endDAte

    a.endDate > c.endDate


Adding your Joins, it should be like this: 添加联接,应该是这样的:

 SELECT c.courseExecutionID, c.beginDate, c.endDate, co.title, co.price, co.title FROM courseExecution c JOIN course co ON c.courseID = co.courseID JOIN availableDates a ON co.courseID = a.courseID WHERE c.courseExecutionID NOT IN (SELECT courseExecutionID FROM participant) AND c.done = 0 AND a.beginDate < c.beginDate AND a.endDate > c.endDate 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM