问个时间查询问题

Mirs 2011-11-07 10:57:18
怎么查某表一次性从2011-10-15号到2011-11-04号的08:30到09:00的全部记录
这样写只能查出这一天的,怎么才能一次性查出呢 ??
select distinct a.test, a.test2, a.test3, a.test4,a.time
from testtime a
where a.test1 in ('11', '22')
and a.time between to_date('2011-10-15 08:30', 'YYYY-mm-dd hh24:mi') and
to_date('2011-10-15 09:00', 'YYYY-mm-dd hh24:mi')
...全文
122 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoqiuwyh 2011-11-10
  • 打赏
  • 举报
回复
between and 可以实现呀。
xiaoyu871225 2011-11-10
  • 打赏
  • 举报
回复
4楼的不行。
你确定在这个时间区间内会有数据??
and a.time >= to_date('2011-10-15 00:00', 'YYYY-mm-dd hh24:mi')
and a.time < to_date('2011-11-05 00:00', 'YYYY-mm-dd hh24:mi')
东方2009 2011-11-10
  • 打赏
  • 举报
回复
SELECT a.test, a.test2, a.test3, a.test4,a.time
FROM testtime a
WHERE a.test1 in ('11', '22')
AND (a.time BETWEEN TO_DATE('2011-10-15 00:00', 'YYYY-mm-dd hh24:mi')
AND TO_DATE('2011-11-05 00:00', 'YYYY-mm-dd hh24:mi'))
AND TO_CHAR(a.time,'hh24:mi') between '08:30' and '09:00' ;
zhangqin12356 2011-11-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xiaobn_cn 的回复:]
SQL code


select distinct a.test, a.test2, a.test3, a.test4,a.time
from testtime a
where a.test1 in ('11', '22')
and a.time >= to_date('2011-10-15 00:00', 'YYYY-mm-dd hh24:mi')
and a……
[/Quote]

4楼的方法挺好的,学习了。
Mirs 2011-11-09
  • 打赏
  • 举报
回复
4楼答案挺好的。。给你了。
Mr傅 2011-11-09
  • 打赏
  • 举报
回复
4L已经给出正确答案了,而且清晰易懂。
xiaobn_cn 2011-11-07
  • 打赏
  • 举报
回复

select distinct a.test, a.test2, a.test3, a.test4,a.time
from testtime a
where a.test1 in ('11', '22')
and a.time >= to_date('2011-10-15 00:00', 'YYYY-mm-dd hh24:mi')
and a.time < to_date('2011-11-05 00:00', 'YYYY-mm-dd hh24:mi')
and to_char(a.time,'hh24:mi') between '08:30' and '09:00' ;

funfenffun 2011-11-07
  • 打赏
  • 举报
回复

where trunc(a.time,'dd') between to_date('2011-10-15', 'YYYY-mm-dd') and to_date('2011-11-04', 'YYYY-mm-dd')
and
a.time-trunc(a.time,'dd') between to_date('2011-10-15 08:30', 'YYYY-mm-dd hh24:mi')-to_date('2011-10-15', 'YYYY-mm-dd') and to_date('2011-10-15 09:00', 'YYYY-mm-dd hh24:mi')-to_date('2011-10-15', 'YYYY-mm-dd')
yixilan 2011-11-07
  • 打赏
  • 举报
回复
select distinct a.test, a.test2, a.test3, a.test4,a.time
from testtime a,
(select to_date('20111015083000', 'yyyymmddhh24miss')+rownum-1 as begintime,
to_date('20111015090000', 'yyyymmddhh24miss')+rownum-1 as endtime
from dual
connect by rownum<=to_date('20111104', 'yyyymmdd')-to_date('20111015', 'yyyymmdd')
)b
where a.test1 in ('11', '22')
and a.time between b.begintime and b.endtime;
psufnxk2000 2011-11-07
  • 打赏
  • 举报
回复
多写几个or 试试
xiedi1209 2011-11-07
  • 打赏
  • 举报
回复
这个方法非常值得学习to_char(a.time,'hh24:mi')
yinan9 2011-11-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xiaobn_cn 的回复:]
SQL code

select distinct a.test, a.test2, a.test3, a.test4,a.time
from testtime a
where a.test1 in ('11', '22')
and a.time >= to_date('2011-10-15 00:00', 'YYYY-mm-dd hh24:mi')
and a.t……
[/Quote]+1
这个逻辑比较清晰,无需绕弯
我心飞翔 2011-11-07
  • 打赏
  • 举报
回复
貌似SQL语句无法从数据库中查询出符合这种条件的记录,用存储过程可以实现。
或者从数据库中查询出某些天的记录,然后在编程语言中对小时分进行字符串解析
来确定是否为符合条件的记录。
我心飞翔 2011-11-07
  • 打赏
  • 举报
回复
经过实际测试#6楼的无法得出正确结果。
BenChiM888 2011-11-07
  • 打赏
  • 举报
回复

select distinct a.test, a.test2, a.test3, a.test4,a.time
from testtime a
where a.test1 in ('11', '22')
and a.time >= to_date('2011-10-04 00:00', 'YYYY-mm-dd hh24:mi')
and a.time < to_date('2011-11-16 00:00', 'YYYY-mm-dd hh24:mi')
and to_char(a.time,'hh24:mi') between '08:30' and '09:00' ;

cutebear2008 2011-11-07
  • 打赏
  • 举报
回复
犀利!简洁才是美啊!
[Quote=引用 4 楼 xiaobn_cn 的回复:]

SQL code

select distinct a.test, a.test2, a.test3, a.test4,a.time
from testtime a
where a.test1 in ('11', '22')
and a.time >= to_date('2011-10-15 00:00', 'YYYY-mm-dd hh24:mi')
and a.time ……
[/Quote]

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧