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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:00:16+00:00 2026-06-02T22:00:16+00:00

I know title is really ridiculous. I couldn’t find how I can describe this.

  • 0

I know title is really ridiculous. I couldn’t find how I can describe this.

This is my table structure
enter image description here

This is a car rental webpage. What I’m doing is I’m trying to figure out if a car is reserved in particular “pickup_date“ and “dropoff_date”.
I can get reserved cars with this query

SELECT
cars.model,
reservations.pickup_date,
reservations.dropoff_date,
reservations.car_id
FROM
cars
INNER JOIN reservations ON cars.car_id = reservations.car_id
WHERE
reservations.pickup_date <= '2012-04-08 10:50:26' OR
reservations.dropoff_date >= '2012-04-20 10:50:26'

However, there are some cars that are not reserved and they are not shown in this query, since I don’t have all car models in reservation. I have them in cars table.

The bottom line, I should eliminate the ones that I get from this query from the table that has all car models. Therefore, I will have cars which are not reserved + the ones that will be available after reserve dates.

It will get the results that I got from the query, and eliminate this results from cars table. After that It will show me.

I can do it with PHP. However, I want to learn if it is possible to do it with one query with mysql.

This is the solution that I came up with with the help of @cbuckley

SELECT
cars.*
FROM cars
LEFT JOIN (
    SELECT
    cars.car_id
    FROM
    cars
    INNER JOIN reservations
    ON cars.car_id = reservations.car_id
    WHERE
    (reservations.pickup_date <= '2012-04-08 10:50:26' AND reservations.dropoff_date >= '2012-04-20 10:50:26') OR
    (reservations.pickup_date >= '2012-04-08 10:50:26' AND reservations.pickup_date <= '2012-04-20 10:50:26') OR
    (reservations.dropoff_date <= '2012-04-20 10:50:26' AND reservations.dropoff_date >= '2012-04-08 10:50:26')
) AS reserved_cars
ON reserved_cars.car_id = cars.car_id
WHERE reserved_cars.car_id IS NULL;
  • 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-06-02T22:00:24+00:00Added an answer on June 2, 2026 at 10:00 pm

    You need to be careful distinguishing cars with no reservation at a given time and cars with a reservation at a time other than the given time. The former is what you’re after and could be achieved with a WHERE NOT IN clause. However as your cars and reservations system grows, this could become hideously slow. I would suggest the following:

    SELECT
    cars.*
    FROM cars
    LEFT JOIN (
        SELECT
        cars.car_id
        FROM
        cars
        INNER JOIN reservations
        ON cars.car_id = reservations.car_id
        WHERE
        reservations.pickup_date <= '2012-04-08 10:50:26' OR
        reservations.dropoff_date >= '2012-04-20 10:50:26'
    ) AS reserved_cars
    ON reserved_cars.car_id = cars.car_id
    WHERE reserved_cars.car_id IS NULL;
    

    The inner query is the one you specified to find all reserved cars. You then join that back to the cars table to find all cars which are not returned by the inner query. This works because you perform a LEFT JOIN to the subquery and only return rows which didn’t have a match in the subquery.

    EDIT: If you want the query to find all cars which are available for a given period, your inner query needs to search for reservations which start or end within that period. You would therefore use the following condition:

    ... WHERE
    reservations.pickup_date  BETWEEN '2012-04-08 10:50:26' AND '2012-04-20 10:50:26' OR
    reservations.dropoff_date BETWEEN '2012-04-08 10:50:26' AND '2012-04-20 10:50:26'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry about the vague title, but I really don't know how to describe this
i didn't really know how to title this question, but here's a thing that
Yes I know, this title isn't really helpfull but this is the exact problem.
I really don't know how to title this question, but I need some help
Sorry for the poor title, I don't really know what to call this. I
Sorry everyone, I really can't find any better title for my question. I have
I really didn't know what title to give this question, but I'll explain here:
I know this is a really strange title, but I'll do my best to
I don't know how to give this a better title as I don't really
Sorry, I really don't know how to summarize the title of this question. So,

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.