Skip to content
Advertisement

Check one data lies within range of two date field in mysql

I have this data:

enter image description here

And, I am trying to compare that one of the date is within range of the given data, but without any success. Here is my query:

SELECT * FROM tbl_emp_leave_details
WHERE emp_id          =  81
AND leave_start_date  >= '2019-09-03'
AND leave_end_date    <= '2019-09-03'

The type of both column is set to date.

Update:

 $bindings   = array(
            ":leave_start_date" => $switchDate,
            ":leave_end_date"   => $switchDate,
            ":empId"            => $row["EmployeeNamePK"]
        );


        $sqlValidation =
            "SELECT * FROM tbl_emp_leave_details
             WHERE emp_id          =  :empId
             AND :leave_start_date >= leave_start_date
             AND :leave_end_date   <= leave_end_date";

Advertisement

Answer

Your are using wrong logic here. As per your requirement, your input date value should fall BETWEEN the leave_start_date and leave_end_date. So , switch your logic to other way around:

SELECT * FROM tbl_emp_leave_details
WHERE emp_id          =  81 
  AND '2019-09-03' BETWEEN leave_start_date AND leave_end_date

One can also write this query using >= and <= operators. Conceptually, the input date value should be greater than or equal to the leave_start_date. Also, for it to fall within the define range, it should be less than or equal to the leave_end_date:

SELECT * FROM tbl_emp_leave_details
WHERE emp_id          =  81
AND '2019-09-03'  >= leave_start_date
AND '2019-09-03'  <= leave_end_date
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement