2017-03-02 56 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