时间查询
要查询一个记事表里面的信息,包含一个时间字段:
2013-12-26
2013-12-28
2012-12-31
...
查询要求:
1.今天是2013-12-26,那么就查询出 2013-12-26 的信息;
2.明天是27号,那么就查询出2013-12-28(未来时间离27号最近的一个时间)的信息;
3.到28号的时候,还是查询2013-12-28的信息;
4.到29号的时候就查询到数据库里面时间比29号最近的一个时间 2012-12-31 的信息
.....
就是始终查询距离当天最近的未来一个时间的信息
求大神指点下,怎么处理,在线等了= =
[解决办法]
with t as
(select to_date('2013/10/10', 'yyyy/mm/dd') date1, '1' nam
from dual
union all
select to_date('2013/10/12', 'yyyy/mm/dd') date1, '2' nam
from dual
union all
select to_date('2013/10/15', 'yyyy/mm/dd') date1, '3' nam from dual)
select *
from t
where date1 = decode((select count(1)
from t
where t.date1 = to_date('2013/10/11', 'yyyy/mm/dd')),
0,
(select min(date1)
from t
where t.date1 > to_date('2013/10/11', 'yyyy/mm/dd')),
to_date('2013/10/11', 'yyyy/mm/dd'))
WITH T AS
(SELECT TO_DATE('2013/12/10', 'yyyy/mm/dd') DATE1
FROM DUAL
UNION ALL
SELECT TO_DATE('2013/12/26', 'yyyy/mm/dd') DATE1
FROM DUAL
UNION ALL
SELECT TO_DATE('2013/12/29', 'yyyy/mm/dd') DATE1
FROM DUAL)
SELECT (SELECT MIN(T.DATE1) FROM T WHERE T.DATE1 >= SYSDATE) FROM DUAL;
WITH T AS
(SELECT TO_DATE('2013/12/10', 'yyyy/mm/dd') DATE1
FROM DUAL
UNION ALL
SELECT TO_DATE('2013/12/26', 'yyyy/mm/dd') DATE1
FROM DUAL
UNION ALL
SELECT TO_DATE('2013/12/29', 'yyyy/mm/dd') DATE1
FROM DUAL)
SELECT (SELECT MIN(T.DATE1)
FROM T
WHERE TO_CHAR(T.DATE1, 'yyyymmdd') >= TO_CHAR(SYSDATE, 'yyyymmdd'))
FROM DUAL;