日期:2014-05-17  浏览次数:20762 次

关于时间范围问题
页面输入2个字段 生效和到期日期 数据库也有这2个字段 如何判断页面输入的时间范围不在数据库时间范围内
并且不能有交集,如页面输入2012-02-02,2012-08-08 数据库的范围是2012-01-01,2012-09-09
返回false 用sql和java 都可以 望解答 谢谢
另页面的时间范围传入后台,到期日期必定大于生效日期,后台数据库同样

如:数据库范围2012-01-01和2012-09-09

用户页面输入2012-01-02和2012-10-10 返回false
  2012-10-10和2012-12-10 返回true
  2012-03-03和2012-08-08 返回fasle

------解决方案--------------------
晕,和这个逻辑不一样的,差的远了

SQL code

select count(*) from tableName t 
where (to_date('2012-08-19','yyyy-mm-dd') not between t.startdate and t.enddate)
and (to_date('2012-09-20','yyyy-mm-dd') not between t.startdate and t.enddate)
and (t.startdate not between to_date('2012-08-19','yyyy-mm-dd') and to_date('2012-09-20','yyyy-mm-dd'));

------解决方案--------------------
昨天没看清题。
似乎是要更新有效期,新的有效期不能和旧的有效期重复,下面这个也可以吧(前提是数据库里面的enddate>startdate):
select × from tablename t
where to_date('2012-08-19','yyyy-mm-dd')< to_date('2012-09-20','yyyy-mm-dd')
and (to_date('2012-08-19','yyyy-mm-dd') > t.enddate 
or to_date('2012-09-20','yyyy-mm-dd') < t.startdate)

如果新的有效期不能比旧的有效期旧,可以把最后一行的 or 去掉。