How to create PHP insert query based on selected timing

I want to create an insert query based on the selected start time, end timing and duration of the session. This is for an appointment table.

$start_time = 9.am $end_time = 11.am $per_session = 30 mins 

Based on the above options I need to create an insert query.

Expected result

 S.no   time      1.      9 am  2.      9.30 am   3.      10.00 am  4.      10.30 am  5.      11.00 am 
Add Comment
1 Answer(s)

One option uses a recursive query – available in MySQL 8.0 only:

insert into mytable (sno, time) with recursive cte as (     select 1 sno, '09:00:00' time     union all     select sno + 1, time + interval 30 minute      from cte      where time + interval 30 minute <= '11:00:00' ) select sno, time from cte 

You can easily turn this to a parameterized query to pass the three parameters as needed.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.