2017-03-02 61 views
-1

我有一个条件,如不要让用户申请已经在数据库中的日期。我在数据库中有一个日期Start_date-> 01-01-2017 & end_date-> 05-01-2017。当用户自带以下日期,我必须显示错误消息限制mysql中的日期

  1. startDate-> 2017年5月1日EndDate-> 2017年6月1日
  2. StartDate-> 31-12-2016 endaDate-> 2017年1月1日
  3. StartDate-> 31-12-2016结束日期 - > 2017年6月1日
  4. StartDate-> 2017年2月1日结束日期 - > 2017年2月1日
  5. StartDate-> 04 -01-2017 endDate-> 06-01-2017

我试过此查询

SELECT * from leave_request_tbl l 
where leave_start_date between '2017-01-01' and '2017-01-05' 
and leave_end_date between '2017-01-01' 
and '2017-01-05' 
order by user_id; 

我不能够满足我的条件,怎么办得到事先就

感谢

+1

日期常量的正确格式为'2017-01-05'。 –

+2

错误的'order by user_id = 2'右'AND user_id = 2 ORDER BY user_id' – RiggsFolly

+1

mysql日期格式为YYYY-MM-DD –

回答

1

如果要检查用户(ID = 2)输入startDate-> 05-01-2017 EndDate->06-01-2017那么重叠的行用户提供的间隔:

select * 
from leave_request_tbl l 
where leave_start_date <= '2017-01-06' 
and leave_end_date >= '2017-01-05' 
and user_id=2 
order by leave_start_date ; 

区间i1和i2重叠如果i1.start <= i2.end and i2.start <= i1.end

+0

它不符合我的2,3,4条件。但我已经完成了Java代码。不管怎么说,还是要谢谢你.. – jai