简体   繁体   中英

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

I have this data:

在此处输入图片说明

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";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM