Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1066369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:00:13+00:00 2026-05-16T20:00:13+00:00

Suppose we have this table.. CREATE TABLE `appointments` ( `idappointments` int(11) NOT NULL AUTO_INCREMENT,

  • 0

Suppose we have this table..

CREATE TABLE `appointments` (
  `idappointments` int(11) NOT NULL AUTO_INCREMENT,
  `timeStart` time DEFAULT NULL,
  `timeEnd` time DEFAULT NULL,
  PRIMARY KEY (`idappointments`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8$$

assumption

Suppose that a range between timeStart and timeEnd cant exist again… I mean that if we intersect all the ranges in the table the result would be EMPTY ,0 , null.An appointment cant cooexist with another..

So what i want to do is a time suggestion if the desired time is occupied…
A proposal before and a proposal after the desired time….

So i was wondering instead of writing code to do this if i could write an SQL query to find the nearest empty ranges……

Ex.. timeStart – NEAREST_TO_TIMESTART_TIMEEND >’10 minutes’ whereas 10 minutes the duration

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:00:13+00:00Added an answer on May 16, 2026 at 8:00 pm

    MySQL doesn’t have recursive functionality, so you’re left with using the NUMBERS table trick –

    1. Create a table that only holds incrementing numbers – easy to do using an auto_increment:

      DROP TABLE IF EXISTS `example`.`numbers`;
      CREATE TABLE  `example`.`numbers` (
        `id` int(10) unsigned NOT NULL auto_increment,
         PRIMARY KEY  (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
      
    2. Populate the table using:

      INSERT INTO NUMBERS
        (id)
      VALUES
        (NULL)
      

      …for as many values as you need.

    3. Use DATE_ADD to construct a list of dates, increasing the days based on the NUMBERS.id value. Replace “2010-01-01” and “2010-01-02” with your respective start and end dates (but use the same format, YYYY-MM-DD HH:MM:SS) –

      SELECT x.dt
        FROM (SELECT TIME(DATE_ADD('2010-01-01', INTERVAL (n.id - 1) * 15 MINUTE)) AS dt
                FROM numbers n
               WHERE DATE_ADD('2010-01-01', INTERVAL (n.id - 1) * 15 MINUTE) <= '2010-01-02' ) x
      
    4. LEFT JOIN onto your table of data based on the datetime portion.

    This will show you the first available slot:

           SELECT x.dt
          FROM (SELECT TIME(DATE_ADD('2010-01-01', INTERVAL (n.id - 1) * 15 MINUTE)) AS dt
                  FROM numbers n
                 WHERE DATE_ADD('2010-01-01', INTERVAL (n.id - 1) * 15 MINUTE) <= '2010-01-02' ) x
        LEFT JOIN APPOINTMENTS a ON x.dt BETWEEN a.timestart AND a.timeend
            WHERE a.idappoinment IS NULL
              AND x.dt > @your_minimum_datetime
         ORDER BY x.dt
            LIMIT 1
    

    This will show you availability for the entire day:

           SELECT x.dt,
                  CASE 
                    WHEN a.idappoinment IS NULL THEN 'available'
                    ELSE 'booked'
                  END AS isbooked 
          FROM (SELECT TIME(DATE_ADD('2010-01-01', INTERVAL (n.id - 1) * 15 MINUTE)) AS dt
                  FROM numbers n
                 WHERE DATE_ADD('2010-01-01', INTERVAL (n.id - 1) * 15 MINUTE) <= '2010-01-02' ) x
        LEFT JOIN APPOINTMENTS a ON x.dt BETWEEN a.timestart AND a.timeend
         ORDER BY x.dt
    

    Why Numbers, not Dates?

    Simple – dates can be generated based on the number, like in the example I provided. It also means using a single table, vs say one per data type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have 2 tables, a and b: CREATE TABLE a (id_a INT NOT
Suppose I have a table (MySQL) like this: CREATE TABLE sessions ( session_id INT
Suppose I have a table like CREATE TABLE associacao ( id bigserial NOT NULL,
Suppose I have the following tables: CREATE TABLE Game ( GameID INT UNSIGNED NOT
I have a table like this: create table t1 { person_id int, item_name varchar(30),
Suppose I have a table like that: create table reserved ( id int(4) primary
Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`
Suppose I have this table: drop table if exists t_user; CREATE TABLE t_user (usr
Suppose I have this code create temporary table somedata (a integer); insert into somedata
Suppose that I have table create table foo ( insert_current timestamp default now(), insert_previous

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.