I have been searching for an answer to this dilemma but found nothing. I was hoping you could help to find out what I am doing wrong.
I have a mysql query that selects only apartmens available as follow:
SELECT *
FROM apartments
WHERE apartment_ID NOT IN (SELECT apartment_ID
FROM bookings
WHERE startDate <= '$endingdate'
AND endDate >= '$startingdate')
The problem is that this query is not considering that i.e. departure day is an half day and it is available.
To explain better:
if table "booking" has a booking ending on 16-01-2011 and the search is from the 16-01-2011. The apartment should be available because that is an "half" day (the day of departure). The query seems to not consider this and does not show the apartment.
Please could you help?
Francesco
I'm not that familiar with MySQL data types , but are you sure that startDate and endDate are not stored with a time part as well. If that's the case then "16-01-2011 16:25" would not be less than or equal to "16-01-2011".
Take out the = signs from your sub query, or at least the last one if a place vacated on a date is always available on that date. That should check for apartments being vacated on the requested date. Also make sure that your data is escaped.
If availabilty depends on time then you will need to change your date fields to datetimes.
Related
I am trying to display reservations/bookings details that are between two dates.
I have this calendar :
If I select, that I want to see details of reservations that are "happening" between dates 2020-06-09 and 2020-06-11, I want to get both of the reservations (blue and red). I mean, that it does not matter when the reservation has been made or when it is supposed to end, if it is or was active between those 2 dates, it should be displayed.
So what I need to achieve:
If I select I want to display reservations between dates 2020-06-09 and 2020-06-11, I should get back both red (2020-06-10 --- 2020-06-12) and blue (2020-06-08 --- 2020-06-12) reservations.
Right now I have this MySQL query but it does not seem to work correctly.
SELECT * FROM Bookings WHERE CONVERT('2020-06-09', DATE) BETWEEN booked_from
AND booked_until AND CONVERT('2020-06-11', DATE) BETWEEN booked_from AND booked_until
If I am understanding the question correctly, you are looking to get any database record where the end date is after 2020-06-09 and the start date is before 2020-06-11. This will select any booking that overlaps with your date range, whether it starts before and ends in the range, starts in and ends after the range, starts before and ends after the range, or starts and ends inside the range. In all of these cases, it starts before you're later date and ends after your earlier date. So try this:
SELECT * FROM Bookings WHERE STR_TO_DATE('2020-06-09', '%Y-%m-%d') <= booked_until AND STR_TO_DATE('2020-06-11', '%Y-%m-%d') >= booked_from
Note: I've replaced the CONVERT function with the more explicit STR_TO_DATE function, but your final query may vary slightly depending on the database your using it on and what schema you're using.
if you use a query like below it might work for you:
SELECT * FROM Bookings
WHERE (DATE(booked_from)
BETWEEN DATE('2020-06-09)
AND DATE('2020-06-11))
AND (DATE(booked_until)
BETWEEN DATE('2020-06-09)
AND DATE('2020-06-11))
I'm working on a reservation/booking system for a small hotel. I'm pretty good with PHP but not so good with SQL... I made a form where you enter your information, number of rooms and select arrival date and check-out date using a calendar.
Now everything went good until I got to the point where you have to check which rooms are available and it's giving me a headache. There are 10 rooms you can book.
I currently have one table in MySQL storing the information, dates, booking-ID and room-ID/number.
How would you make the SQL for checking which rooms that are available and not?
Should it look something like
"SELECT * FROM bookings WHERE checkinDate >= '$formCheckin'
AND checkoutDate <= '$formCheckout' "
and then get the roomID and count them?
Any help is very appreciated!
If you want to know if a room is available during a period, then the logic looks like:
SELECT r.*
FROM rooms r
WHERE NOT EXISTS (SELECT 1
FROM bookings b
WHERE b.roomid = r.roomid AND
b.checkinDate <= $formCheckOut AND
b.checkoutDate >= '$formCheckIn
);
I'm not sure if the equality comparison is needed for both of these. The logic is that a room is available if there are no bookings that start before the checkout date and that end after the check in date.
However, for ten rooms, I might suggest that you just keep a table of each room by day, say for the next ten years (add another year once per year). Such a table isn't very big and it is probably easier to understand how to use it. Plus, you can handle things like a couple reserve the room for a week, but only one person is in the room for the first 3 days.
Sorry for asking, but I've never had to do such a complex MYSQL query before and I don't actually know what to google search in order to get the answer.
I have a poorly crafted database with a table of appointments of pregnant women that includes the day they came and the number of weeks pregnant they were at that time. I'm trying to select each one that should be 30 weeks right now but that doesn't already have a separate entry after 25 weeks pregnancy. I use the phone number to uniquely identify each person.
Since I really don't know how to formulate this query, this is the best I've come up with.
SELECT * FROM patientlist WHERE
UNIX_TIMESTAMP() - (UNIX_TIMESTAMP(`date`) - `weekspreg`*604800) > 29*604800
AND
UNIX_TIMESTAMP() - (UNIX_TIMESTAMP(`date`)- `weekspreg`*604800) <= 30*604800
AND
/* a subquery that keeps out results where the phone number would show up elsewhere in the table for a woman with more than 25 weeks of pregnancy. */
There has to be a better solution than separately querying each of the results from the date range by phone number to see if the weekspreg is more than 25.
Thank you in advance for any help or direction.
Your entire WHERE is incorrect. A query can only have ONE where clause. You join multiple conditions with and and or, not and where:
WHERE foo AND bar // correct
WHERE foo AND WHERE bar // syntax error
Check out the MySQL Date and Time Functions. For example, I'm not entirely certain what that last WHERE clause is trying to do, but I believe the first portion could be rewritten as something like:
SELECT *
FROM patientlist
WHERE `date` - interval `weekspreg` week
between now() - interval 29 week
and now() - interval 30 week
I have problem thinking this trough with PHP & Mysql. I have read How to implement check availability in hotel reservation system and some more but they seem different.
I have to make a reservation system on which a converence room can be booked for 6hours minimal, so when a user make a reservation on a date e.g 24/04/2012 he then selects the start time of the rental (00:00-06:00, 06:00-12:00, 12:00-18:00,18:00-24:00). And the total number of hours(6,12,18 etc..) the user wants to rent the room.
The date is stored as a integer(timestamp).
I have two problems with this; I donĀ“t know how to receive all possible days of a month on which the room is still up for reservation and how to check if a new reservation is possible.
I know how to calculate the start date and end date on the users input but I cant find the correct mysql query or php check for availability.
It's probably really simple but somehow because the end date is always variable I just cant find the answer.
Thanks in advance guys!
EDIT Table structure: reservations
id int(11)
user_id int(11)
reservation_name varchar(255)
start_date int(11)
end_date int(11)
I believe reservations is the only one relevant
You'll find it's pretty difficult to generate a list of available days in MySQL. I recommend instead that you select an ordered list of booked days within your desired month, then loop over all days of that month in PHP skipping the day if it matches the next booked day from your MySQL query. The answer to this question will help you to build the dates over which you want to loop in PHP. In pseudocode:
$booked_days = sql(select all booked days in month order by day);
for each $day in month {
if $day != current($booked_days) {
// $day is not booked
} else advance_next($booked_days);
}
To check if a new reservation is possible, you might want to have a look at my answer to a very similar question earlier today.
This brilliant simple MySQL query must do the trick:
Checking for overlapping car reservations
This solution is only for "dates" but You can simply aggregate it with additional "hours"
I'm creating a time off requester for my business and I think I've run into a small issue.
Goal:
I want to display a page that will show me if anyone has request the current day off.
Problem:
I have the date_from and date_to fields which contain the information of the start and end dates for each request off. However, and I apologize if I'm just over thinking (under thinking?) the solution, but I cannot for the life of me figure out how to compare if today's current date (yyyy-mm-dd) intersects with any of the current records in my database. I've tried toying around with the between query but haven't had much luck.
Any assistance would be greatly appreciated!
SELECT * FROM TheTable
WHERE CURDATE() BETWEEN date_from AND date_to