Having Problem fetching rows from mysql table between two date columns - mysql

I am having the table and i want to fetch rows based on 'rb_rm_id' column between the 2 dates 'rb_chkin' and 'chkout'. But, It gives me 0 result even if the values are present. Please help me. Thanks in Advance.
I have Tried the Following Mysql Query but, failed:
SELECT *
FROM room_booking
WHERE `rb_rm_id` = 2 AND
(`rb_chkin` >= '2019-08-05' AND `rb_chkin` <= '2019-08-06') AND
(`rb_chkout` >= '2019-08-05' AND `rb_chkout` <= '2019-08-06');
The Table image is here [1]: https://imgur.com/eQZ1I0C "Table"

Question is not clear and also your mysql table doesn't contains any value in that range. There is no check in value in your given range.
try this one
SELECT *
FROM room_booking
WHERE `rb_rm_id` = 2 AND
(`rb_chkin` <= '2019-08-05' AND `rb_chkout` >= '2019-08-06');

Your query says:
Give me any rows where rb_chkin and rb_chkout both are between 2019-08-05 and 2019-08-06 and the rb_rm_id is 2.
There is no such row in your table.
What you probably want is to detect whether a room is booked during the given date range. For a room to be, at least partially, occupied there can three case:
The check-in date falls within the date range.
The check-out date falls within the date range.
The check-in date is before the date range, and the check-out is after the date range.
If either one of these three conditions is met the room has a booking during the given date range. The equivalent query would be:
SELECT *
FROM room_booking
WHERE `rb_rm_id` = 2 AND
(`rb_chkin` BETWEEN '2019-08-05' AND '2019-08-06') OR
(`rb_chkout` BETWEEN '2019-08-05' AND '2019-08-06') OR
(`rb_chkin` < '2019-08-05' AND `rb_chkout` > '2019-08-06');
So this returns all rows that have a booking for the given room in the given date range.
Please consider using better column names, which would make your code actually readable. Something like: rb_room_id, rb_customer_id, rb_check_in_date, rb_check_out_date. I really don't understand this need for obfuscation.

Related

Check hall avaiblity by date range in mysql where condition

My problem is that i want to count of id's between two dates range. But my written query is not give me required answer. sample is below .
select COUNT(hall_id) as countids
from comm_hall_form
where hall_id ='13' and ((date(booked_from)
between '2019-11-08' and '2019-11-09') or (date(booked_to)
between '2019-11-08' and '2019-11-09'))
You need to check if the booked time fully or partially covers the period you are asking for. Right now you are only checking if it starts or ends with that period.
SELECT COUNT(id) as countids
FROM comm_hall_form
WHERE hall_id ='13'
(DATE(booked_from) <= '2019-11-09' AND DATE(booked_to) >= '2019-11-09')
OR
(DATE(booked_from) <= '2019-11-08' AND DATE(booked_to) >= '2019-11-08')

Error when runing query from form between two date from a form

I have a form called FirstInLastOut which looks as the image below.
Based on Name or badge number I want to search between two dates.
I am using the following criteria on the query:
>=[Forms]![FirstInLastOut]![StartDateEntry] And <=[Forms]![FirstInLastOut]![EndDateEntry]
This is given me results that include other months as well. Please see the query report below.
So as you can see in the image the numbers of the dates are falling with the the parameter but getting other months as well.
How can I make it so it will only select the dates between the date ranges?
SELECT FistClockInRaw.Badgenumber, FistClockInRaw.name, FistClockInRaw.lastname, FistClockInRaw.MinOfCHECKTIME, FLastClockOutRaw.MaxOfCHECKTIME, [MaxOfCHECKTIME]-[MinOfCHECKTIME] AS TotalHours, FLastClockOutRaw.QDate, FistClockInRaw.MinOfQTime, FLastClockOutRaw.MaxOfQTime, RawCompleteQuery.CHECKTIME
FROM RawCompleteQuery, FLastClockOutRaw INNER JOIN FistClockInRaw ON (FLastClockOutRaw.Badgenumber = FistClockInRaw.Badgenumber) AND (FLastClockOutRaw.name = FistClockInRaw.name) AND (FLastClockOutRaw.QDate = FistClockInRaw.QDate)
WHERE (((FistClockInRaw.name)=[Forms]![FirstInLastOut]![FirstNameEntry]) AND ((RawCompleteQuery.CHECKTIME)>=[Forms]![FirstInLastOut]![StartDateEntry] And (RawCompleteQuery.CHECKTIME)<=[Forms]![FirstInLastOut]![EndDateEntry]));
is the Query
I assume that the forms fields StartDateEntry and EndDateEntry are bound to fields of type date.
I also assume that you are only interested to compare the date part of those form fields.
So try this condition instead to assure correct date interpreting:
WHERE FistClockInRaw.name=[Forms]![FirstInLastOut]![FirstNameEntry]
AND RawCompleteQuery.CHECKTIME >= Format([Forms]![FirstInLastOut]![StartDateEntry], "\#yyyy-mm-dd\#")
AND RawCompleteQuery.CHECKTIME <= Format([Forms]![FirstInLastOut]![EndDateEntry], "\#yyyy-mm-dd\#")
A remark:
Be aware that every date field/variable always contains a time part too!
So your current logic comparing EndDateEntry with <= can cause trouble, because you would only get results of the end date having time values of 00:00:00 in the field CHECKTIME.
If any record of CHECKTIME contains the requested end date and a time part bigger then 00:00:00, it is not in the result.
To avoid that, you should use < and add one day:
And RawCompleteQuery.CHECKTIME < Format([Forms]![FirstInLastOut]![EndDateEntry] + 1, "\#yyyy-mm-dd\#")

mysql filter by date and time separately

I have to perform a query on a MySQL database.
I have a table with records, have a column called "date" (the date type), and a column called "time" (type. Integer is stored by multiplying the time of day by 60. eg 8 am is stored as 480).
Unfortunately, the format of this table can not be modified.
My table stores attentions of doctors on call. The doctors on duty working in two shifts: from 8-20, and 20-8.
I need to know the amount of attention for every doctor.
My query must be filtered by date range and shift.
The problem is that, in the case of doctors working at the turn of 20-8, I have to consider a change of day. (sorry for my bad English).
What I have done is this, this would be an example to date of yesterday, and doctors shift 20-8.
SELECT * FROM attentions WHERE (date >= '2015-07-23' and time >=1200) and (date <= '2015-07-24' and time <480)
the query does not work at all.
Supposing the date field is called: 'a_date' with format 'yyyy-mm-ss' and the time field is a number, the query should be:
SELECT * FROM attentions WHERE (date(a_date) >= '2015-07-23' and time >=1200) and (date(a_date) <= '2015-07-24' and time <480)
Can you check using between?
SELECT * FROM attentions WHERE date between '2015-07-23' and '2015-07-24' and time between 1200 and 480
I think you can also use this -
SELECT * FROM ***** where CREATED_DATETIME between '2015-03-12 00:00:00' and '2015-05-11 00:00:00';

Select leave data from attendance table given the following condition

I have attendance data for employees stored in the table attendance with the following column names:
emp_id (employee ID)
date
type (leave, absent, etc.)
(there are others but I'm omitting them for the sake of simplicity)
My objective is to retrieve all dates of the given month on which the employee was on leave (type = 'Leave') and the last leave taken in the last month, if any.
It's easy to do it using two queries (I'm using PHP to get process the data), but is there any way this can be done in a single query?
I'm answering my own question so as to close it. As #bpgergo pointed out in the comments, UNION will do the trick here.
SELECT * FROM table_name
WHERE type="Leave" AND
date <= (CURRENT_DATE() - 30)
Select the fields, etc you want then se a combined where clause using mysql's CURRENT_DATE() function. I subtracted 30 for 30 days in a month.
If date is a date column, this will return everyone who left 1 month or longer ago.
Edit:
If you want a specific date, change the 2nd month like this:
date <= (date_number - 30)

Filtering the dates range based on the days

I have a table with columns as
Id (1)
Date holding values as (2013-12-12 00:00:00)
Start time (00:00:00)
end time (01:00:00)
value
The user will give the date range and will specify day like sunday ,monday etc .
How can I use a sql query to filter the dates and find the proper days between that for the specified days as well.
One way would be to use BETWEEN ... AND ... and DAYOFWEEK():
SELECT *
FROM my_table
WHERE Date BETWEEN ? AND ?
AND DAYOFWEEK(Date) = ? -- 1=Sunday ... 7=Saturday