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 7064631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:51:29+00:00 2026-05-28T04:51:29+00:00

I am writing a hotel booking system. after lots of studying (including stack overflow)

  • 0

I am writing a hotel booking system. after lots of studying (including stack overflow) i wrote this sql to find out free rooms:

SELECT
*
FROM room
WHERE
    room_id NOT IN (
        SELECT room_id
        FROM bookings
        WHERE
                 checkin <= '$check_in'
            AND checkout >= '$check_out'
    )

but the problem is its not considering the time checking is 12:00:00 and checkout is 11:59:00

also its not giving the right queries like within the date range its not working like if I book from 15-18 a single room who’s number is 501. if I again run a query 17-19 this rooms seems free but in reality it should be occupied.

can anyone suggest a very good and effective sql which will get the accurate date so that no clash will happen booking system because the system will be implemented in real so mistakes will cause many issues.

thanks in advance

  • 1 1 Answer
  • 0 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-28T04:51:29+00:00Added an answer on May 28, 2026 at 4:51 am

    The problem you’re having is that your query is not sufficiently robust. When you break down the problem, what you have is this:

    If the range defined by $check_in and $check_out overlaps the range defined by checkin and checkout in any way, then the room is booked. Otherwise, it is free.

    This means that:

    • If $check_in >= checkin and $check_in <= checkout, the room is BOOKED
    • OR If $check_out >= checkin and $check_out <= checkout, the room is BOOKED
    • OR If $check_in <= checkin and $check_out >= checkout, the room is BOOKED

    So, you need to represent both of these scenarios in your subquery in order to get the information you’re looking for.

    Also, you will hopefully be using datetime for your comparisons and not just time, otherwise you will have side effects.

    EDIT: SQL Query

    (Keep in mind that there is more than one way to skin a cat, so to speak. I’m just providing an example that keeps with what you already have as much as possible. Once again, I’m also assuming that checkin, checkout, $check_in, and $check_out will all resolve to datetime types)

    SELECT *
    FROM room
    WHERE room_id NOT IN
    (SELECT room_id 
     FROM bookings
     WHERE
       (checkin <= '$check_in' AND checkout >= '$check_in') OR
       (checkin <= '$check_out' AND checkout >= '$check_out') OR
       (checkin >= '$check_in' AND checkout <= '$check_out'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a program for class that manages a Hotel. This Report1 function
Writing the code for the user authentication portion of a web site (including account
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
Writing my first Linq application, and I'm trying to find the best way to
Writing a python program, and I came up with this error while using the
Writing a SQL query that should return the average charges for a preceding 91
writing in QT and QErrorMessage by default has checkbox saying: Show this message again
I am writing an SQL query which will return a list of auctions a
Writing a 4-player iPhone game app with programmable mix of human/autoComputer players. After each

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.