在mysql中检查一个数据位于两个日期字段的范围内

时间:2019-09-11 08:08:36

标签: mysql sql

我有此数据:

enter image description here

而且,我正在尝试比较一个日期是否在给定数据的范围内,但没有成功。这是我的查询:

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

两列的类型均设置为日期。

更新:

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

1 个答案:

答案 0 :(得分:2)

您在这里使用错误的逻辑。根据您的要求,您的输入日期值应分别为BETWEENleave_start_dateleave_end_date。因此,将逻辑切换为其他方式:

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

还可以使用>=<=运算符编写此查询。从概念上讲,输入日期值应大于或等于leave_start_date。另外,要使其落在定义范围内,它应小于或等于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