3

I have table called booked meeting and following is the table structure

meeting table

id | meeting_name | from_time  | to_time | date

Following are the structure

CREATE TABLE `bookings` (
  `id` int(11) NOT NULL,
  `meeting_name` varchar(255) DEFAULT NULL,
  `from_time` time DEFAULT NULL,
  `to_time` time DEFAULT NULL,
  `date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



INSERT INTO `bookings` (`id`, `meeting_name`, `from_time`, `to_time`, `date`) VALUES(
(1, 'meeting1', '09:00:00', '10:30:00', '2018-07-26'),
(2, 'meeting2', '11:40:00', '12:25:00', '2018-07-26');
(3, 'meeting3', '03:40:00', '04:25:00', '2018-07-26'););

and here i am trying find available slots between two time suppose if i enter from time as 10:00:00 and to time as 01:00:00 then it should return all the meetings between this time and need to find out available timing between them

I am new to mysql so tried few queries but didn't got expected result

SELECT * 
FROM `bookings` 
WHERE bookings.from_time>="10:00:00" and bookings.to_time<="01:00:00"


SELECT * 
FROM `bookings` 
WHERE bookings.from_time>="10:00:00" or
    bookings.to_time<="10:00:00" or
    bookings.from_time>="01:00:00" or
    bookings.to_time<="01:00:00"

Expected Result if i pass time between 10:00:00.00 to 13:00:00 then it should return

enter image description here

and also available slots like

10:31:00 to 11:39:00
12.26 to 03:49:00
The Impaler
  • 45,731
  • 9
  • 39
  • 76
scott
  • 3,112
  • 19
  • 52
  • 90

2 Answers2

0

To find all meetings with at least some of their time within the defined time slot (from 10:00h to 13:00h) you should do

SELECT * 
FROM `bookings` 
WHERE bookings.from_time<="13:00:00" -- end of time slot
AND   bookings.to_time>=  "10:00:00" -- start of time slot

I hope that I understood the question properly with respect to the end time. I prefer to use the 24h notation. For completeness you should also check that the date value is identical to the investigated time slot.

Try out the little demo here: http://rextester.com/LNUD45826

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
0

Firstly, I used 24h format, you should too.

Here's your data enriched by one additional day, to show, that query will encapsulate gaps between meetings within the same day:

CREATE TABLE `bookings` (
  `id` int(11) NOT NULL,
  `meeting_name` varchar(255) DEFAULT NULL,
  `from_time` time DEFAULT NULL,
  `to_time` time DEFAULT NULL,
  `date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `bookings` (`id`, `meeting_name`, `from_time`, `to_time`, `date`) VALUES
(1, 'meeting1', '09:00:00', '10:30:00', '2018-07-25'),
(2, 'meeting2', '11:40:00', '12:25:00', '2018-07-25'),
(3, 'meeting3', '03:40:00', '04:25:00', '2018-07-25'),
(1, 'meeting4', '09:00:00', '10:30:00', '2018-07-26'),
(2, 'meeting5', '11:40:00', '12:25:00', '2018-07-26'),
(3, 'meeting6', '03:40:00', '04:25:00', '2018-07-26');

And here's your query, it's easy with LAG function implemented in SQL Server, here we have to imitate it (here's how to do it - it will help you understand the query)

select @start := cast('10:00:00' as time), @end := cast('13:00:00' as time),
       @time := cast(null as time), @date := cast(null as date);

select date, to_time_lag, from_time, gap_between_meetings from (
  select id,
         meeting_name,
         from_time,
         @time to_time_lag,
         to_time,
         @date date_lag,
         case when @date = date then timediff(from_time, @time) end gap_between_meetings,
         @time := to_time,
         date,
         @date := date
  from bookings
  where from_time between @start and @end
     or to_time between @start and @end
  order by date, from_time
) a where gap_between_meetings is not null
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69