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))
Related
I have a parent object, let's say an Event.
This event can occur on several dates, so we have EventDates.
Now I want to have all Events where the Event's start date is inside a date range (for example a school year).
Normally that would be easy, but since the start date is not a direct property of the Event, we need to join on the EventDates.
I already tried the following (unorthodox) method, which seemed to have worked a while, but suddenly doesn't anymore:
SELECT *, min(ed_date) AS ed_date FROM events
LEFT JOIN event_dates ON events.e_id = event_dates.ed_event_id
WHERE ed_date >= '2016-09-01'
AND ed_date <= '2017-08-31'
Apparently, when there's an EventDate within the range above, it will take the first one in that range as the "min(ed_date)" instead of the actual minimum date.
If there's already a solution posted to this, I must've missed it.
Any help is appreciated. I'd hate to have to use php to find all the separate dates and use those ID's to apply an IN array filter with those results.
Separately saving the start date in the event itself may also be an option, but I don't like redundant data.
If you have a question, or something's unclear, be sure to ask.
Update.
There was a question for some sample data, hope I'm doing this right:
table events:
e_id:int(11) e_category_id:int(11) e_title:varchar(255) e_status:int(11)
table event_dates:
ed_id:int(11) ed_event_id:int(11) ed_title:varchar(255) ed_date:date
Let's say there are 2 events: E1 & E2. (titles, categories & statuses aren't really relevant for this case)
3 event_dates are linked to each of these events.
Dates for E1: 03/05/2017 - 05/05/2017 - 08/05/2017
Dates for E2: 28/04/2017 - 02/05/2017 - 04/05/2017
Now I want all events of which the start date of that event (respectively 03/05/2017 & 28/04/2017) is within the date range 01/05/2017-31/05/2017.
If I do the following:
SELECT *, min(ed_date) as ed_date FROM events
LEFT JOIN event_dates ON event_dates.ed_event_id = events.e_id
WHERE ed_date >= '2017-05-01'
AND ed_date <= '2017-05-31'
GROUP BY events.e_id
(The GROUP BY wasn't included in the first select above, apologies.)
Then I get both events, the ed_date of the second event being 02/05/2017.
It got the min(ed_date) of the dates that were in the range (03/05/2017 & 02/05/2017), instead of the actual min(ed_date) (03/05/2017 & 28/04/2017).
Is it clear like this?
In my page I show some entries based on the range of the chosenTime a user chosen to get contacted, and saved it into mySQL.
I have 4 range groups of times (our working times 09:00-17:00) that a user can select from
09:00-11:00
11:00-13:00
13:00-15:00
15:00-17:00
For now, I show the results as below, order by the chosenTime
$sql = "SELECT * FROM table order by chosenTime asc";
What I want to do is to sort the results based on the current time.
For example if time is 10:30, show first the rows that the chosen time is between 09:00-11:00.
If it is 11:00, show first the 11:00 - 13:00 results.
I am thinking about a between but I don't know on how to proceed with this.
Any help is appreciated
Assuming that chosenTime is character type, in a fixed and canonical format, it's not at all difficult.
We need the current time in the format hh:mm (e.g. 10:30) to match the stored format.
If we want to get current time from the database in that format, that's a simple expression:
DATE_FORMAT(NOW(),'%h:%i')
The ugly part is going to be splitting the character string. Given a fixed format hh:mm-hh:mm, we can use simple SUBSTR functions:
SUBSTR(ct,1,5) -- begin
SUBSTR(ct,7,5) -- end
You could definitely use a BETWEEN comparator, but note that on the boundaries, when the current time is on the edge (e.g. 13:00), we'll get a "match" on both the 11:00-13:00 and the 13:00-15:00 periods.
ORDER
BY DATE_FORMAT(NOW(),'%h:%i') BETWEEN SUBSTR(ct,1,5) AND SUBSTR(ct,7,5) DESC
, ct
, ...
Note that the first expression is evaluated as a boolean, and will return either a 1 (true), 0 (false), or NULL (unknown). We add the DESC keyword so that the true conditions (returned as a value of 1) will be sorted before the false.
This isn't the normal pattern we use for dealing with time values, but this will work for chosenTime in a fixed and canonical format. (This works with the 24-hour clock, but woululdn't work with a 12-hour clock, or single digit hours, etc. That is, for this to work, we have to guarantee that a string comparison is equivalent to a time comparison.)
The first method that would come to mind for me would be to make sub-tables where time is either between the values or not. Something like
Select *
FROM
(Select *, 1 as priority FROM table where time between STARTTIME and ENDTIME) UNION
(Select *, 2 as priority FROM table where time NOT between STARTTIME and ENDTIME)
ORDER BY priority
This should give you results with the results relevant to the current STARTTIME and ENDTIME of your working period first.
I have a table that inserts a row when a certain function is used on the site and stamps the date.
code-/----date----/--type
-----/------------/------
--1--/--2012-2-1--/-used
--1--/--2012-2-3--/-saved
--1--/--2012-1-3--/-printed
--2--/--2012-2-1--/-used
--2--/--2012-2-2--/-printed
I have to report the number of times code 1 was (printed or saved or used) today, or yesterday, or last month (date range)
I am starting with this:
$stat_query = mysql_query("SELECT code, type, date FROM tracking WHERE code IN ('$htL','$htG','$htR') GROUP BY code, type, date");
I use the IN operand because each user has 3 codes to track with limitless date entries for each type.
I really am lost as to what to do here.
You'll need to construct the date ranges and modify the query. Below is an example of showing the count for code in the past day:
SELECT `code`, COUNT(`code`) as code_cnt
FROM tracking
WHERE
`code` IN ('$htL','$htG','$htR') AND
`date` BETWEEN DATE_SUB(now(), INTERVAL 1 HOUR) AND now()
GROUP BY `code`
Check out the DATE_SUB documentation for further help
GROUP BY code, type, date in this example makes it to return only one record. Because all code rows would be summerized in distinct output row, also type would be reduce more and more. Please check your GROUP BY fields.
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.
I have a list of dates in a table in a MySQL database (the dates when a charity bookstall is to be held), which I want to display on a page. On one page I'm displaying the date of the next stall, and on another the dates of the stall in the next month. (Currently I'm using an unordered HTML list and selecting the dates with PHP, but it's a bit messy, and I also want to tie in the dates with the fundraising totals that are stored in the database).
I want to put the dates in a database though so that I can tie in the dates with the fundraising totals for each week. I'm thinking that once I can identify the date with the nearest up-coming date that I can use 'LIMIT 1' to select the next week's date for display, and 'LIMIT 4' say for where I need to display the dates for the next month, but what I can't figure out is how to identify the record with the nearest up-coming date - identifying the current date and then selecting the nearest date...I have a feeling there's probably one of the MySQL date functions that can be persuaded to help out in this, but can't figure out exactly how.
Any ideas on how I can do this?
If I understand correctly, you can just pick up next four dates that are after today.
In MySQL you could use the CURDATE() function for the 'today' bit, then apply an order and limit to your select statement. For example,
SELECT stall_date
FROM stall_dates
WHERE stall_date >= CURDATE() -- >= assumes you want today's to show too
ORDER BY
stall_date
LIMIT 4
Use ORDER BY stall_date DESC to reverse the ordering if needed.
If your column is a DATETIME field, you can identify the next by using SELECT...WHERE event_date > "2009-11-06" and ORDER BY event_date.
SELECT * FROM so_events
WHERE event_date > "2009-11-06 15:36:00"
ORDER BY event_date ASC
LIMIT 4
MySQL will internally do the work for you and select rows where whose timestamp is greater than the one you specify in the WHERE clause.