Mysql BETWEEN clause being ignored. No syntax error - php

im trying to show items booked between two separate date columns (start and end dates). i want to search between the dates but the query seems to ignore any dates that land between the selected dates.
SELECT
bookings.booking_id,
bookings.booking_id,
bookings.booked_from,
bookings.booked_from,
bookings.booked_till,
item_set_computer.item_id,
item_set_computer.name,
item_set_computer.booking_groups_id
FROM
bookings,
item_set_computer
WHERE
item_set_computer.item_id = bookings.item_id
AND
item_set_computer.booking_groups_id = 3
AND
booked_from
BETWEEN "2010-04-13" AND "2010-04-20"
AND
booked_till
BETWEEN "2010-04-13" AND "2010-04-20"
for instance, I have an item booked from 13th to 15th. date1 is 2010-04-13 and date2 is 2010-04-15. User searches for booked items from 14th to 16th. It returns no results. Why is it ignoring database dates that drop between dates selected by the user? The columns are set as DATE in the database and have been correctly entered.

You said "User searches for booked items from 14th to 16th."
That means your query will be
AND
booked_from
BETWEEN "2010-04-14" AND "2010-04-16"
AND
booked_till
BETWEEN "2010-04-14" AND "2010-04-16"
The first AND clause will obviously be false (since your 4/13 start date is NOT indeed between 4/14 and 4/16)
The correct logic for you (assuming you want the FULL booking interval) is
AND booked_from >= "2010-04-14"
AND booked_till <= "2010-04-16"
Or if you want partial interval
AND booked_from >= "2010-04-16" -- means we booked before the end of interval
AND booked_till <= "2010-04-14" -- means we left after the start

are there recordsets with the same id in both table which falls under the clause? also make sure that with WHERE col BETWEEN a AND b, a has to be smaller than b to work.
what gets returned if you comment out the last 6 lines?

Related

Select bookings between 2 dates, even though they did not end yet

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))

show records from sql created during specific week

I have tons of records that are in my database table leadactivity basically I need to display all the records that were created in the first week of the current month, then also another query to display records in the second week, same for third week and then finally the fourth week of the current month.
I have a column called created_date which onupdate puts in the current timestamp
What is the best way to achieve this?
You can use date functions for this:
select la.*
from leadactivity la
where day(la.created_date) between 1 and 7 and
created_date >= curdate() + (1 - day(curdate())) day;
The above assumes MySQL syntax. Most databases have similar functionality, although the specific functions may differ.

Mysql intersection of datetimes

I have the table Vacation in mysql DB. The table has datetime_from and datetime_to. To have a vacation from Monday to Friday means there is only one record in the table with two timestamps
date_from = 'MONDAY_DATE 00:00:00'
date_to = 'FRIDAY_DATE 23:59:59'
The working time is from 8:00 to 16:00 every day. I would like to get all the working time that employee missed during his vacation (in hours for example). It's 8hrs a day x 5.
Im able to do that in 5 queries (one for every day and then sum up with PHP) as an intersection of date intervals BUT is it possible to perform it in only one mysql query?
SELECT SUM(date_to - date_from) as seconds_missed FROM Vacation
WHERE ...
That would give you the total number of seconds missed for an employee assuming the where clause matches it against a given user. I guess you could also add conditions in your where clause to only grab dates in a certain range (i.e. worked missed that week).

calculate total number of days in specied dates

i have one table with two columns as shown in picture
table columns names are (MAXDATE,AMOUNT).
if you see we have
first date range (from current date to 20-jan-2010)
second date range from 20-jan-2010 to 30-jan-2010
3rd range is from 20-jan-2010 to 31-jan-2010.
at the execution of page user enter the start and end date.
for example, if user put
start date: 18-jan-2010
end date: 23-jan-2010
then he has 2 dates in first options and 3 dates in second options.
what i want to calculate in sql
1. how many days in first range (if any this depends on supplied dates from user)
2. how many days in 2nd range (if any this depends on supplied dates from user)
3. how many days in 3rd range (if any this depends on supplied dates from user)
Thanks
Here is an example how to calculate days.
http://dev.mysql.com/doc/refman/5.0/en/date-calculations.html
You can do all of this in MySQL:
SELECT DATEDIFF(NOW(), max_date) as days;
SELECT DATEDIFF(date2, date1) FROM
(SELECT max_date as date1 FROM table1 LIMIT 1) as s1
JOIN
(SELECT max_date as date2 FROM table1 LIMIT 1 OFFSET 1) as s2;
//etc.

Selecting the next date in MySQL

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.

Categories