检查重叠以及是否已取值

我正在尝试编写一个 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)

我试图了解我失败的地方,但我不知道在哪里。预先感谢您的回答。


精慕HU
浏览 154回答 1
1回答

长风秋雁

我认为你采取了错误的方式,对我来说这是一个否定的检查,如果它给你带来超过 0 行,它将失败:是否有任何课程在同一地点同时进行?或者我的老师是否应该同时开设任何其他课程?如您所见,OR您的查询中有一个 I can't find 。(我不明白你Event_id的意思,所以我可能会在这里错过任何东西)在制品这部分至少可以回答你问题的一大部分,告诉我什么仍然不适合你。SQL小提琴查询 1:SET -- this is your input you try to check&nbsp;@local_id = 1,&nbsp;@heure_debut = '08:00',&nbsp;@heure_fin = '09:00',@enseignant_id = 1结果:查询 2:-- if it return more that 0 record, then you have a conflictSELECT&nbsp;&nbsp; r.id AS id_line_in_conflict&nbsp; , r.* -- for debugFROM `reservations` rWHERE&nbsp;&nbsp; heure_debut < @heure_fin&nbsp;&nbsp; AND heure_fin > @heure_debut&nbsp;&nbsp; AND (&nbsp; &nbsp; local_id = @local_id -- check if the local is empty&nbsp; &nbsp; OR enseignant_id = @enseignant_id -- check if the teacher is free&nbsp; &nbsp; )结果:| id_line_in_conflict | id | numero_semaine |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;date | heure_debut | heure_fin | Event_id | horaire_id | local_id | enseignant_id ||---------------------|----|----------------|----------------------|-------------|-----------|----------|------------|----------|---------------||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 |&nbsp; 1 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;16 | 2020-04-17T00:00:00Z |&nbsp; &nbsp; &nbsp; &nbsp;08:00 |&nbsp; &nbsp; &nbsp;12:00 |&nbsp; &nbsp; &nbsp; &nbsp; 1 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4 |&nbsp; &nbsp; &nbsp; &nbsp; 1 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2 |&nbsp; 2 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;16 | 2020-04-17T00:00:00Z |&nbsp; &nbsp; &nbsp; &nbsp;08:00 |&nbsp; &nbsp; &nbsp;09:00 |&nbsp; &nbsp; &nbsp; &nbsp; 1 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4 |&nbsp; &nbsp; &nbsp; &nbsp; 2 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 |查询 3:SET -- this is your input you try to check&nbsp;@local_id = 1,&nbsp;@heure_debut = '14:00',&nbsp;@heure_fin = '15:00',@enseignant_id = 3结果:查询 4:-- if it return more that 0 record, then you have a conflictSELECT&nbsp;&nbsp; r.id AS id_line_in_conflict&nbsp; , r.* -- for debugFROM `reservations` rWHERE&nbsp;&nbsp; heure_debut < @heure_fin&nbsp;&nbsp; AND heure_fin > @heure_debut&nbsp;&nbsp; AND (&nbsp; &nbsp; local_id = @local_id -- check if the local is empty&nbsp; &nbsp; OR enseignant_id = @enseignant_id -- check if the teacher is free&nbsp; &nbsp; )结果:
打开App,查看更多内容
随时随地看视频慕课网APP