Time Slot Booking Script - Check availabilityHow do I check if a string contains a specific word?MySQL representation of time slots for appointmentsHow to structure and query an appointment system based on time slots where each bookable entity has a different time table daily?PHP MySql Time slot searchGetting all available time slot ranges from bookings table in MySQLGenerate empty row for unused slotsMySQL query for available days and time slotsPHP/MySQL - Find available time slotsLogic when checking availability of slot bookings in SQLUnable to connect my sql server from PHP

Why is getting a PhD considered "financially irresponsible" by some people?

Did ancient peoples ever hide their treasure behind puzzles?

What's the point of fighting monsters in Zelda BoTW?

Should an STL container avoid copying elements into themselves when the container is copied into itself?

Unlock your Lock

Defending Castle from Zombies

Given current technology, could TV display screens double as video camera sensors?

A first "Hangman" game in Python

Commercial company wants me to list all prior "inventions", give up everything not listed

Pen test results for web application include a file from a forbidden directory that is not even used or referenced

What to do about my 1-month-old boy peeing through diapers?

Find feasible point in polynomial time in linear programming

How to prevent a hosting company from accessing a VM's encryption keys?

Why does Windows store Wi-Fi passwords in a reversible format?

Do sharpies or markers damage soft gear?

Half filled water bottle

Force SQL Server to use fragmented indexes?

How to say "I only speak one which is English." in French?

What happens to transactions included in stale blocks?

Can a DM change an item given by another DM?

Many many thanks

Talk interpreter

Could the UK amend the European Withdrawal Act and revoke the Article 50 invocation?

How to force GCC to assume that a floating-point expression is non-negative?



Time Slot Booking Script - Check availability


How do I check if a string contains a specific word?MySQL representation of time slots for appointmentsHow to structure and query an appointment system based on time slots where each bookable entity has a different time table daily?PHP MySql Time slot searchGetting all available time slot ranges from bookings table in MySQLGenerate empty row for unused slotsMySQL query for available days and time slotsPHP/MySQL - Find available time slotsLogic when checking availability of slot bookings in SQLUnable to connect my sql server from PHP






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Trying to make a basic booking system where by a user can say they need a 30 minute appointment and the code can output what time slots are free to book for 30 minutes.



I first tried to ignore the "time" factor and code this by "slots" to take all the date time complexity out of it, and started with just making it work for 1 day.



I divided the day up into 15 minute slots and then created a $slotstatus of 1 for busy and 0 for free.



Then was a simple matter of looking through the table rows to find free slots and echo them.



The problem I have is when the time required takes more than 1 slot. SO I then need to count the amount of "Slotstatus = 0" and ensure there is enough available time for the requirement.



I tried using a count ++ when I see a 0 but then had problems on the areas where I needed to output two slots in sequence.



Im very amateur at code and im doing this as a bit of a hobby to boost my coding logic brain which is poor.



Please can someone point me in the right direction



''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



 $count = 0;
$req = 3;
echo $req . " is the SlotReq<br>";
echo $count . " is the starting count<br><br>";




$sql = "SELECT slotnumber, slottime, slotstatus FROM timeslots";
$result = $conn->query($sql);


if ($result->num_rows > 0)

// output data of each row
while($row = $result->fetch_assoc() )

if ($row["slotstatus"] == 0)

$count ++;

if ( $count == $req)



echo "<br>Pass for " . $row["slottime"] . "
Status: ". $row["slotstatus"] . " Count " . $count;


$count = 0;

else

echo "<br> False for " . $row["slottime"] . "
Status: ". $row["slotstatus"] . " Count " . $count;





else

echo "<br> False for " . $row["slottime"] . "
Status: ". $row["slotstatus"] . " Count " . $count;
$count = 0;





else

echo "Error: No results Found";

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


should look like this on the page



Table
table as image
ID slottime slotstatus
1 10:15 0
2 10:30 1
3 10:45 0
4 11:00 0
5 11:15 0
6 11:30 0
7 11:45 1



So if the user wants a 30 minute slot they should be presented with the options based on the table above as



10:45
11:00
11:15










share|improve this question






























    0















    Trying to make a basic booking system where by a user can say they need a 30 minute appointment and the code can output what time slots are free to book for 30 minutes.



    I first tried to ignore the "time" factor and code this by "slots" to take all the date time complexity out of it, and started with just making it work for 1 day.



    I divided the day up into 15 minute slots and then created a $slotstatus of 1 for busy and 0 for free.



    Then was a simple matter of looking through the table rows to find free slots and echo them.



    The problem I have is when the time required takes more than 1 slot. SO I then need to count the amount of "Slotstatus = 0" and ensure there is enough available time for the requirement.



    I tried using a count ++ when I see a 0 but then had problems on the areas where I needed to output two slots in sequence.



    Im very amateur at code and im doing this as a bit of a hobby to boost my coding logic brain which is poor.



    Please can someone point me in the right direction



    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



     $count = 0;
    $req = 3;
    echo $req . " is the SlotReq<br>";
    echo $count . " is the starting count<br><br>";




    $sql = "SELECT slotnumber, slottime, slotstatus FROM timeslots";
    $result = $conn->query($sql);


    if ($result->num_rows > 0)

    // output data of each row
    while($row = $result->fetch_assoc() )

    if ($row["slotstatus"] == 0)

    $count ++;

    if ( $count == $req)



    echo "<br>Pass for " . $row["slottime"] . "
    Status: ". $row["slotstatus"] . " Count " . $count;


    $count = 0;

    else

    echo "<br> False for " . $row["slottime"] . "
    Status: ". $row["slotstatus"] . " Count " . $count;





    else

    echo "<br> False for " . $row["slottime"] . "
    Status: ". $row["slotstatus"] . " Count " . $count;
    $count = 0;





    else

    echo "Error: No results Found";

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


    should look like this on the page



    Table
    table as image
    ID slottime slotstatus
    1 10:15 0
    2 10:30 1
    3 10:45 0
    4 11:00 0
    5 11:15 0
    6 11:30 0
    7 11:45 1



    So if the user wants a 30 minute slot they should be presented with the options based on the table above as



    10:45
    11:00
    11:15










    share|improve this question


























      0












      0








      0








      Trying to make a basic booking system where by a user can say they need a 30 minute appointment and the code can output what time slots are free to book for 30 minutes.



      I first tried to ignore the "time" factor and code this by "slots" to take all the date time complexity out of it, and started with just making it work for 1 day.



      I divided the day up into 15 minute slots and then created a $slotstatus of 1 for busy and 0 for free.



      Then was a simple matter of looking through the table rows to find free slots and echo them.



      The problem I have is when the time required takes more than 1 slot. SO I then need to count the amount of "Slotstatus = 0" and ensure there is enough available time for the requirement.



      I tried using a count ++ when I see a 0 but then had problems on the areas where I needed to output two slots in sequence.



      Im very amateur at code and im doing this as a bit of a hobby to boost my coding logic brain which is poor.



      Please can someone point me in the right direction



      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



       $count = 0;
      $req = 3;
      echo $req . " is the SlotReq<br>";
      echo $count . " is the starting count<br><br>";




      $sql = "SELECT slotnumber, slottime, slotstatus FROM timeslots";
      $result = $conn->query($sql);


      if ($result->num_rows > 0)

      // output data of each row
      while($row = $result->fetch_assoc() )

      if ($row["slotstatus"] == 0)

      $count ++;

      if ( $count == $req)



      echo "<br>Pass for " . $row["slottime"] . "
      Status: ". $row["slotstatus"] . " Count " . $count;


      $count = 0;

      else

      echo "<br> False for " . $row["slottime"] . "
      Status: ". $row["slotstatus"] . " Count " . $count;





      else

      echo "<br> False for " . $row["slottime"] . "
      Status: ". $row["slotstatus"] . " Count " . $count;
      $count = 0;





      else

      echo "Error: No results Found";

      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


      should look like this on the page



      Table
      table as image
      ID slottime slotstatus
      1 10:15 0
      2 10:30 1
      3 10:45 0
      4 11:00 0
      5 11:15 0
      6 11:30 0
      7 11:45 1



      So if the user wants a 30 minute slot they should be presented with the options based on the table above as



      10:45
      11:00
      11:15










      share|improve this question














      Trying to make a basic booking system where by a user can say they need a 30 minute appointment and the code can output what time slots are free to book for 30 minutes.



      I first tried to ignore the "time" factor and code this by "slots" to take all the date time complexity out of it, and started with just making it work for 1 day.



      I divided the day up into 15 minute slots and then created a $slotstatus of 1 for busy and 0 for free.



      Then was a simple matter of looking through the table rows to find free slots and echo them.



      The problem I have is when the time required takes more than 1 slot. SO I then need to count the amount of "Slotstatus = 0" and ensure there is enough available time for the requirement.



      I tried using a count ++ when I see a 0 but then had problems on the areas where I needed to output two slots in sequence.



      Im very amateur at code and im doing this as a bit of a hobby to boost my coding logic brain which is poor.



      Please can someone point me in the right direction



      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



       $count = 0;
      $req = 3;
      echo $req . " is the SlotReq<br>";
      echo $count . " is the starting count<br><br>";




      $sql = "SELECT slotnumber, slottime, slotstatus FROM timeslots";
      $result = $conn->query($sql);


      if ($result->num_rows > 0)

      // output data of each row
      while($row = $result->fetch_assoc() )

      if ($row["slotstatus"] == 0)

      $count ++;

      if ( $count == $req)



      echo "<br>Pass for " . $row["slottime"] . "
      Status: ". $row["slotstatus"] . " Count " . $count;


      $count = 0;

      else

      echo "<br> False for " . $row["slottime"] . "
      Status: ". $row["slotstatus"] . " Count " . $count;





      else

      echo "<br> False for " . $row["slottime"] . "
      Status: ". $row["slotstatus"] . " Count " . $count;
      $count = 0;





      else

      echo "Error: No results Found";

      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


      should look like this on the page



      Table
      table as image
      ID slottime slotstatus
      1 10:15 0
      2 10:30 1
      3 10:45 0
      4 11:00 0
      5 11:15 0
      6 11:30 0
      7 11:45 1



      So if the user wants a 30 minute slot they should be presented with the options based on the table above as



      10:45
      11:00
      11:15







      php mysql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 20:25









      Daniel PereiraDaniel Pereira

      31 bronze badge




      31 bronze badge

























          1 Answer
          1






          active

          oldest

          votes


















          0















          You can use a query like this. It count first the free slots and the you
          can query the requested slots in the outer SELECT:



          SELECT * from (
          SELECT ts.*
          , @sclotcount := IF( ts.`slotstate` = 1, 0, @sclotcount +1 ) as freeslots
          FROM timeslots ts
          CROSS JOIN ( SELECT @sclotcount := 0) as init
          ORDER by id DESC
          ) as result
          WHERE freeslots >= 3 -- request slots
          ORDER by id;


          SAMPLE



          MariaDB [test]> SELECT * from ( SELECT ts.*, @sclotcount := IF( ts.`slotstate` = 1, 0, @sclotcount +1 ) as freeslots FROM timeslots ts CROSS JOIN ( SELECT @sclotcount := 0) as init ORDER by id DESC ) as result WHERE freeslots >= 3 ORDER by id;
          +----+----------+-----------+-----------+
          | id | slottime | slotstate | freeslots |
          +----+----------+-----------+-----------+
          | 1 | 00:00:00 | 0 | 3 |
          | 10 | 02:15:00 | 0 | 3 |
          | 14 | 03:15:00 | 0 | 7 |
          | 15 | 03:30:00 | 0 | 6 |
          | 16 | 03:45:00 | 0 | 5 |
          | 17 | 04:00:00 | 0 | 4 |
          | 18 | 04:15:00 | 0 | 3 |
          | 22 | 05:15:00 | 0 | 75 |
          | 23 | 05:30:00 | 0 | 74 |
          | 24 | 05:45:00 | 0 | 73 |
          ...
          | 83 | 20:30:00 | 0 | 14 |
          | 84 | 20:45:00 | 0 | 13 |
          | 85 | 21:00:00 | 0 | 12 |
          | 86 | 21:15:00 | 0 | 11 |
          | 87 | 21:30:00 | 0 | 10 |
          | 88 | 21:45:00 | 0 | 9 |
          | 89 | 22:00:00 | 0 | 8 |
          | 90 | 22:15:00 | 0 | 7 |
          | 91 | 22:30:00 | 0 | 6 |
          | 92 | 22:45:00 | 0 | 5 |
          | 93 | 23:00:00 | 0 | 4 |
          | 94 | 23:15:00 | 0 | 3 |
          +----+----------+-----------+-----------+
          80 rows in set (0.001 sec)

          MariaDB [test]>





          share|improve this answer

























          • Just trying to make this work with my VERY limited MYSQL knowledge, Cross join, does this imply I have a separate table called ts like the one above with freeslots? or am i being stupid?

            – Daniel Pereira
            Mar 28 at 20:30












          • FROM timeslots ts means only that ts is a alias for the table name timeslots. its only a shorter notation. so you can write ts.fieldname instead of timeslots.fieldname . CROSS JOIN is only a helper (in this case) to initialize the user variable ** sclotcount**.. if you not initialize you cant add like @sclotcount +1

            – Bernd Buffen
            Mar 28 at 20:35











          • You can direcly run this query on your server

            – Bernd Buffen
            Mar 28 at 20:36











          • THANK you that worked like a dream, I dont know why but it didnt even cross my mind to do all of the calculation on MYSQL query.

            – Daniel Pereira
            Mar 31 at 18:36










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385871%2ftime-slot-booking-script-check-availability%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0















          You can use a query like this. It count first the free slots and the you
          can query the requested slots in the outer SELECT:



          SELECT * from (
          SELECT ts.*
          , @sclotcount := IF( ts.`slotstate` = 1, 0, @sclotcount +1 ) as freeslots
          FROM timeslots ts
          CROSS JOIN ( SELECT @sclotcount := 0) as init
          ORDER by id DESC
          ) as result
          WHERE freeslots >= 3 -- request slots
          ORDER by id;


          SAMPLE



          MariaDB [test]> SELECT * from ( SELECT ts.*, @sclotcount := IF( ts.`slotstate` = 1, 0, @sclotcount +1 ) as freeslots FROM timeslots ts CROSS JOIN ( SELECT @sclotcount := 0) as init ORDER by id DESC ) as result WHERE freeslots >= 3 ORDER by id;
          +----+----------+-----------+-----------+
          | id | slottime | slotstate | freeslots |
          +----+----------+-----------+-----------+
          | 1 | 00:00:00 | 0 | 3 |
          | 10 | 02:15:00 | 0 | 3 |
          | 14 | 03:15:00 | 0 | 7 |
          | 15 | 03:30:00 | 0 | 6 |
          | 16 | 03:45:00 | 0 | 5 |
          | 17 | 04:00:00 | 0 | 4 |
          | 18 | 04:15:00 | 0 | 3 |
          | 22 | 05:15:00 | 0 | 75 |
          | 23 | 05:30:00 | 0 | 74 |
          | 24 | 05:45:00 | 0 | 73 |
          ...
          | 83 | 20:30:00 | 0 | 14 |
          | 84 | 20:45:00 | 0 | 13 |
          | 85 | 21:00:00 | 0 | 12 |
          | 86 | 21:15:00 | 0 | 11 |
          | 87 | 21:30:00 | 0 | 10 |
          | 88 | 21:45:00 | 0 | 9 |
          | 89 | 22:00:00 | 0 | 8 |
          | 90 | 22:15:00 | 0 | 7 |
          | 91 | 22:30:00 | 0 | 6 |
          | 92 | 22:45:00 | 0 | 5 |
          | 93 | 23:00:00 | 0 | 4 |
          | 94 | 23:15:00 | 0 | 3 |
          +----+----------+-----------+-----------+
          80 rows in set (0.001 sec)

          MariaDB [test]>





          share|improve this answer

























          • Just trying to make this work with my VERY limited MYSQL knowledge, Cross join, does this imply I have a separate table called ts like the one above with freeslots? or am i being stupid?

            – Daniel Pereira
            Mar 28 at 20:30












          • FROM timeslots ts means only that ts is a alias for the table name timeslots. its only a shorter notation. so you can write ts.fieldname instead of timeslots.fieldname . CROSS JOIN is only a helper (in this case) to initialize the user variable ** sclotcount**.. if you not initialize you cant add like @sclotcount +1

            – Bernd Buffen
            Mar 28 at 20:35











          • You can direcly run this query on your server

            – Bernd Buffen
            Mar 28 at 20:36











          • THANK you that worked like a dream, I dont know why but it didnt even cross my mind to do all of the calculation on MYSQL query.

            – Daniel Pereira
            Mar 31 at 18:36















          0















          You can use a query like this. It count first the free slots and the you
          can query the requested slots in the outer SELECT:



          SELECT * from (
          SELECT ts.*
          , @sclotcount := IF( ts.`slotstate` = 1, 0, @sclotcount +1 ) as freeslots
          FROM timeslots ts
          CROSS JOIN ( SELECT @sclotcount := 0) as init
          ORDER by id DESC
          ) as result
          WHERE freeslots >= 3 -- request slots
          ORDER by id;


          SAMPLE



          MariaDB [test]> SELECT * from ( SELECT ts.*, @sclotcount := IF( ts.`slotstate` = 1, 0, @sclotcount +1 ) as freeslots FROM timeslots ts CROSS JOIN ( SELECT @sclotcount := 0) as init ORDER by id DESC ) as result WHERE freeslots >= 3 ORDER by id;
          +----+----------+-----------+-----------+
          | id | slottime | slotstate | freeslots |
          +----+----------+-----------+-----------+
          | 1 | 00:00:00 | 0 | 3 |
          | 10 | 02:15:00 | 0 | 3 |
          | 14 | 03:15:00 | 0 | 7 |
          | 15 | 03:30:00 | 0 | 6 |
          | 16 | 03:45:00 | 0 | 5 |
          | 17 | 04:00:00 | 0 | 4 |
          | 18 | 04:15:00 | 0 | 3 |
          | 22 | 05:15:00 | 0 | 75 |
          | 23 | 05:30:00 | 0 | 74 |
          | 24 | 05:45:00 | 0 | 73 |
          ...
          | 83 | 20:30:00 | 0 | 14 |
          | 84 | 20:45:00 | 0 | 13 |
          | 85 | 21:00:00 | 0 | 12 |
          | 86 | 21:15:00 | 0 | 11 |
          | 87 | 21:30:00 | 0 | 10 |
          | 88 | 21:45:00 | 0 | 9 |
          | 89 | 22:00:00 | 0 | 8 |
          | 90 | 22:15:00 | 0 | 7 |
          | 91 | 22:30:00 | 0 | 6 |
          | 92 | 22:45:00 | 0 | 5 |
          | 93 | 23:00:00 | 0 | 4 |
          | 94 | 23:15:00 | 0 | 3 |
          +----+----------+-----------+-----------+
          80 rows in set (0.001 sec)

          MariaDB [test]>





          share|improve this answer

























          • Just trying to make this work with my VERY limited MYSQL knowledge, Cross join, does this imply I have a separate table called ts like the one above with freeslots? or am i being stupid?

            – Daniel Pereira
            Mar 28 at 20:30












          • FROM timeslots ts means only that ts is a alias for the table name timeslots. its only a shorter notation. so you can write ts.fieldname instead of timeslots.fieldname . CROSS JOIN is only a helper (in this case) to initialize the user variable ** sclotcount**.. if you not initialize you cant add like @sclotcount +1

            – Bernd Buffen
            Mar 28 at 20:35











          • You can direcly run this query on your server

            – Bernd Buffen
            Mar 28 at 20:36











          • THANK you that worked like a dream, I dont know why but it didnt even cross my mind to do all of the calculation on MYSQL query.

            – Daniel Pereira
            Mar 31 at 18:36













          0














          0










          0









          You can use a query like this. It count first the free slots and the you
          can query the requested slots in the outer SELECT:



          SELECT * from (
          SELECT ts.*
          , @sclotcount := IF( ts.`slotstate` = 1, 0, @sclotcount +1 ) as freeslots
          FROM timeslots ts
          CROSS JOIN ( SELECT @sclotcount := 0) as init
          ORDER by id DESC
          ) as result
          WHERE freeslots >= 3 -- request slots
          ORDER by id;


          SAMPLE



          MariaDB [test]> SELECT * from ( SELECT ts.*, @sclotcount := IF( ts.`slotstate` = 1, 0, @sclotcount +1 ) as freeslots FROM timeslots ts CROSS JOIN ( SELECT @sclotcount := 0) as init ORDER by id DESC ) as result WHERE freeslots >= 3 ORDER by id;
          +----+----------+-----------+-----------+
          | id | slottime | slotstate | freeslots |
          +----+----------+-----------+-----------+
          | 1 | 00:00:00 | 0 | 3 |
          | 10 | 02:15:00 | 0 | 3 |
          | 14 | 03:15:00 | 0 | 7 |
          | 15 | 03:30:00 | 0 | 6 |
          | 16 | 03:45:00 | 0 | 5 |
          | 17 | 04:00:00 | 0 | 4 |
          | 18 | 04:15:00 | 0 | 3 |
          | 22 | 05:15:00 | 0 | 75 |
          | 23 | 05:30:00 | 0 | 74 |
          | 24 | 05:45:00 | 0 | 73 |
          ...
          | 83 | 20:30:00 | 0 | 14 |
          | 84 | 20:45:00 | 0 | 13 |
          | 85 | 21:00:00 | 0 | 12 |
          | 86 | 21:15:00 | 0 | 11 |
          | 87 | 21:30:00 | 0 | 10 |
          | 88 | 21:45:00 | 0 | 9 |
          | 89 | 22:00:00 | 0 | 8 |
          | 90 | 22:15:00 | 0 | 7 |
          | 91 | 22:30:00 | 0 | 6 |
          | 92 | 22:45:00 | 0 | 5 |
          | 93 | 23:00:00 | 0 | 4 |
          | 94 | 23:15:00 | 0 | 3 |
          +----+----------+-----------+-----------+
          80 rows in set (0.001 sec)

          MariaDB [test]>





          share|improve this answer













          You can use a query like this. It count first the free slots and the you
          can query the requested slots in the outer SELECT:



          SELECT * from (
          SELECT ts.*
          , @sclotcount := IF( ts.`slotstate` = 1, 0, @sclotcount +1 ) as freeslots
          FROM timeslots ts
          CROSS JOIN ( SELECT @sclotcount := 0) as init
          ORDER by id DESC
          ) as result
          WHERE freeslots >= 3 -- request slots
          ORDER by id;


          SAMPLE



          MariaDB [test]> SELECT * from ( SELECT ts.*, @sclotcount := IF( ts.`slotstate` = 1, 0, @sclotcount +1 ) as freeslots FROM timeslots ts CROSS JOIN ( SELECT @sclotcount := 0) as init ORDER by id DESC ) as result WHERE freeslots >= 3 ORDER by id;
          +----+----------+-----------+-----------+
          | id | slottime | slotstate | freeslots |
          +----+----------+-----------+-----------+
          | 1 | 00:00:00 | 0 | 3 |
          | 10 | 02:15:00 | 0 | 3 |
          | 14 | 03:15:00 | 0 | 7 |
          | 15 | 03:30:00 | 0 | 6 |
          | 16 | 03:45:00 | 0 | 5 |
          | 17 | 04:00:00 | 0 | 4 |
          | 18 | 04:15:00 | 0 | 3 |
          | 22 | 05:15:00 | 0 | 75 |
          | 23 | 05:30:00 | 0 | 74 |
          | 24 | 05:45:00 | 0 | 73 |
          ...
          | 83 | 20:30:00 | 0 | 14 |
          | 84 | 20:45:00 | 0 | 13 |
          | 85 | 21:00:00 | 0 | 12 |
          | 86 | 21:15:00 | 0 | 11 |
          | 87 | 21:30:00 | 0 | 10 |
          | 88 | 21:45:00 | 0 | 9 |
          | 89 | 22:00:00 | 0 | 8 |
          | 90 | 22:15:00 | 0 | 7 |
          | 91 | 22:30:00 | 0 | 6 |
          | 92 | 22:45:00 | 0 | 5 |
          | 93 | 23:00:00 | 0 | 4 |
          | 94 | 23:15:00 | 0 | 3 |
          +----+----------+-----------+-----------+
          80 rows in set (0.001 sec)

          MariaDB [test]>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 21:04









          Bernd BuffenBernd Buffen

          10.7k2 gold badges16 silver badges24 bronze badges




          10.7k2 gold badges16 silver badges24 bronze badges















          • Just trying to make this work with my VERY limited MYSQL knowledge, Cross join, does this imply I have a separate table called ts like the one above with freeslots? or am i being stupid?

            – Daniel Pereira
            Mar 28 at 20:30












          • FROM timeslots ts means only that ts is a alias for the table name timeslots. its only a shorter notation. so you can write ts.fieldname instead of timeslots.fieldname . CROSS JOIN is only a helper (in this case) to initialize the user variable ** sclotcount**.. if you not initialize you cant add like @sclotcount +1

            – Bernd Buffen
            Mar 28 at 20:35











          • You can direcly run this query on your server

            – Bernd Buffen
            Mar 28 at 20:36











          • THANK you that worked like a dream, I dont know why but it didnt even cross my mind to do all of the calculation on MYSQL query.

            – Daniel Pereira
            Mar 31 at 18:36

















          • Just trying to make this work with my VERY limited MYSQL knowledge, Cross join, does this imply I have a separate table called ts like the one above with freeslots? or am i being stupid?

            – Daniel Pereira
            Mar 28 at 20:30












          • FROM timeslots ts means only that ts is a alias for the table name timeslots. its only a shorter notation. so you can write ts.fieldname instead of timeslots.fieldname . CROSS JOIN is only a helper (in this case) to initialize the user variable ** sclotcount**.. if you not initialize you cant add like @sclotcount +1

            – Bernd Buffen
            Mar 28 at 20:35











          • You can direcly run this query on your server

            – Bernd Buffen
            Mar 28 at 20:36











          • THANK you that worked like a dream, I dont know why but it didnt even cross my mind to do all of the calculation on MYSQL query.

            – Daniel Pereira
            Mar 31 at 18:36
















          Just trying to make this work with my VERY limited MYSQL knowledge, Cross join, does this imply I have a separate table called ts like the one above with freeslots? or am i being stupid?

          – Daniel Pereira
          Mar 28 at 20:30






          Just trying to make this work with my VERY limited MYSQL knowledge, Cross join, does this imply I have a separate table called ts like the one above with freeslots? or am i being stupid?

          – Daniel Pereira
          Mar 28 at 20:30














          FROM timeslots ts means only that ts is a alias for the table name timeslots. its only a shorter notation. so you can write ts.fieldname instead of timeslots.fieldname . CROSS JOIN is only a helper (in this case) to initialize the user variable ** sclotcount**.. if you not initialize you cant add like @sclotcount +1

          – Bernd Buffen
          Mar 28 at 20:35





          FROM timeslots ts means only that ts is a alias for the table name timeslots. its only a shorter notation. so you can write ts.fieldname instead of timeslots.fieldname . CROSS JOIN is only a helper (in this case) to initialize the user variable ** sclotcount**.. if you not initialize you cant add like @sclotcount +1

          – Bernd Buffen
          Mar 28 at 20:35













          You can direcly run this query on your server

          – Bernd Buffen
          Mar 28 at 20:36





          You can direcly run this query on your server

          – Bernd Buffen
          Mar 28 at 20:36













          THANK you that worked like a dream, I dont know why but it didnt even cross my mind to do all of the calculation on MYSQL query.

          – Daniel Pereira
          Mar 31 at 18:36





          THANK you that worked like a dream, I dont know why but it didnt even cross my mind to do all of the calculation on MYSQL query.

          – Daniel Pereira
          Mar 31 at 18:36








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385871%2ftime-slot-booking-script-check-availability%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현