php - Two IF statements and join MYSQL -
i`ve been trying make dayplanner meeting room. have 2 tables:
1) dayplanner
: day split in 5 minute intervals values=00:00:00,00:05:00,00:10:00 ... 23:55:00
2) reservations
: name, start , finish values=[john,12:00:00,12:30:00],[bob,14:00:00,14:45:00],etc
my goal use 1 query 2 if statements can check starttime , lies in between
like this:
[time] [reserved] [inbetween]
12:00:00 true true
12:05:00 true true
............................
12:35:00 false false
$query = "select *, if(d.dayplanner_time = a.reservations_starttime,'true','false') reserved, if(d.dayplanner_time > a.reservations_start && d.dayplanner_time < a.reservations_finish,'true','false') inbetween dayplanner d left join reservations on d.day_time = a.reservations_start or d.dayplanner_time = a.reservations_finish d.dayplanner_time between '12:00:00' , '18:00:00';";
the query gives 1 true in reserverd column nothing works. hope can me.
you're matching time intervals against reservations either start or end exactly on interval. definition, if times equal can't greater nor less themselves, means you're explicitly leaving out reservations don't match time interval.
the comparison should be
select d.dayplanner_time,d.day_time, if(d.dayplanner_time = a.reservations_starttime,'true','false') reserved, if(a.reservations_start null, false, true) inbetween dayplanner d left join reservations on (d.day_time >= a.reservations_start , d.dayplanner_time <= a.reservations_finish) d.dayplanner_time between '12:00:00' , '18:00:00';
which means "if there's @ least 1 reservation starting before me, and ending before me, there's in between".
remember when doing left join, not null means match clause. that's why in_between false when reservations start null.
Comments
Post a Comment