为了账号安全,请及时绑定邮箱和手机立即绑定

检查重叠以及是否已取值

检查重叠以及是否已取值

PHP
精慕HU 2022-12-23 16:34:16
我正在尝试编写一个 SQL 查询,该查询必须检查两个小时之间是否存在重叠以及是否已经选择了某些值。例如,你在一个表中有一条记录,你需要用数据填充一个表单。使用 SQL 查询,您需要检查表中的小时数与您在表单中选择的小时数之间是否存在重叠,但您还需要检查表中选择的其他数据是否尚未在表中使用与表中的另一个数据。我的问题是检查值是否已经在表中,这样我就可以返回一个错误,指出这些数据已被使用。明确地说,您有那些需要验证的数据。你有时间、房间、老师和课程。您必须检查是否检测到时间重叠(我对这部分没问题),但您还需要检查老师是否已经在某个房间里教授某门课程(您不能让老师在两个不同的地方),如果一个房间还没有被占用,也如果一门课程还没有被占用。我实际上有这个 SQL 查询,但它不是一个正确的查询,因为当我用两个小时(例如:08:00 和 12:00)、一个老师(例如:老师 A)、一个房间(例如:房间 A)进行插入时和课程(例如:课程 A),没有问题,因为它是表中的第一个插入。但是当我在不换老师的情况下换房间(A房间到B房间)检查是否会报错(因为老师不能同时在两个不同的地方),有' 没有错误,并且在表中进行了插入。我会给你我的 SQL 查询:select * from `reservations`where heure_debut <= '08:00' and heure_fin >= '08:00' and heure_debut <= '09:00' and heure_fin >= '09:00' and reservations.local_id = 1 and exists (select * from `reservations` where reservations.enseignant_id = 1) and exists (select * from `reservations` where reservations.Event_id = 1)我试图了解我失败的地方,但我不知道在哪里。预先感谢您的回答。
查看完整描述

1 回答

?
长风秋雁

TA贡献1757条经验 获得超7个赞

我认为你采取了错误的方式,对我来说这是一个否定的检查,如果它给你带来超过 0 行,它将失败:


是否有任何课程在同一地点同时进行?

或者我的老师是否应该同时开设任何其他课程?

如您所见,OR您的查询中有一个 I can't find 。(我不明白你Event_id的意思,所以我可能会在这里错过任何东西)


在制品

这部分至少可以回答你问题的一大部分,告诉我什么仍然不适合你。


SQL小提琴

查询 1:


SET -- this is your input you try to check 

@local_id = 1, 

@heure_debut = '08:00', 

@heure_fin = '09:00',

@enseignant_id = 1

结果


查询 2:


-- if it return more that 0 record, then you have a conflict

SELECT 

  r.id AS id_line_in_conflict

  , r.* -- for debug

FROM `reservations` r

WHERE 

  heure_debut < @heure_fin 

  AND heure_fin > @heure_debut 

  AND (

    local_id = @local_id -- check if the local is empty

    OR enseignant_id = @enseignant_id -- check if the teacher is free

    )

结果


| id_line_in_conflict | id | numero_semaine |                 date | heure_debut | heure_fin | Event_id | horaire_id | local_id | enseignant_id |

|---------------------|----|----------------|----------------------|-------------|-----------|----------|------------|----------|---------------|

|                   1 |  1 |             16 | 2020-04-17T00:00:00Z |       08:00 |     12:00 |        1 |          4 |        1 |             1 |

|                   2 |  2 |             16 | 2020-04-17T00:00:00Z |       08:00 |     09:00 |        1 |          4 |        2 |             1 |

查询 3:


SET -- this is your input you try to check 

@local_id = 1, 

@heure_debut = '14:00', 

@heure_fin = '15:00',

@enseignant_id = 3

结果


查询 4:


-- if it return more that 0 record, then you have a conflict

SELECT 

  r.id AS id_line_in_conflict

  , r.* -- for debug

FROM `reservations` r

WHERE 

  heure_debut < @heure_fin 

  AND heure_fin > @heure_debut 

  AND (

    local_id = @local_id -- check if the local is empty

    OR enseignant_id = @enseignant_id -- check if the teacher is free

    )

结果



查看完整回答
反对 回复 2022-12-23
  • 1 回答
  • 0 关注
  • 153 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号