我有两张表
表A name,start_date,end_date
表B tName,re_date表A中的 值全部是固定的 现在要查询tName 并且选中的时间 re_date 在 start_date,end_date 范围内
表A name,start_date,end_date
表B tName,re_date表A中的 值全部是固定的 现在要查询tName 并且选中的时间 re_date 在 start_date,end_date 范围内
我是这样写的 但是用不了
select name,start_date,end_date,tName,re_date from A,B
where to_date(re_date,'yyyy-mm-dd') between to_date(start_date,'yyyy-mm-dd') and to_date(end_date,'yyyy-mm-dd')
and to_date(re_date,'yyyy-mm-dd') =to_date('2011-3-23','yyyy-mm-dd')
可能写的不对 那位帮忙给改下
start_date,end_date 格式也是date类型的数据,并且数据库中start_date,end_date有值
select name,start_date,end_date,tName,re_date from A,B
where to_char(re_date,'yyyy-mm-dd') between to_char(start_date,'yyyy-mm-dd') and to_char(end_date,'yyyy-mm-dd')
and to_char(re_date,'yyyy-mm-dd') ='2011-3-23'
试下~~~
表A,B中都有 多个人的记录,并且表A给了有效期,start_date(开始时间),end_date(结束时间),当我给re_date一个时间时,查询是表B中在这个所有人在这个时间段的tName(名字)
select * from B B where tName in(select name from A A where to_char(B.re_date,'yyyy-mm-dd') between to_char(A.start_date,'yyyy-mm-dd') and to_char(A.end_date,'yyyy-mm-dd'))
那我要给end_date 赋上值咋做呢,上面的语句吧所有的数据都查出来了,查询 re_date 赋值为 2011-9-16 的所有人。
可能还是有点没理解我的意思吧,表A start_date(开始时间),end_date(结束时间)
举个例子:name start_date(开始时间)end_date(结束时间)
xiaoa 2009-10-1 2020-9-1
xiaob 2010-3-2 2010-4-2
xiaoc 2011-11-2 2011-12-9给 表B的 re_date 赋值为 2011-9-16
要查询出来的结果:xiaoa,xiaob 这个时间没在xiaoc的范围内
select name,start_date,end_date,tName,re_date from A,B
where to_date(re_date,'yyyy-mm-dd') between to_date(start_date,'yyyy-mm-dd') and to_date(end_date,'yyyy-mm-dd')
and name = tname
from a t1
where exists (select 1 from b t2
where t2.tName=t1.name
and t2.re_date>=t1.start_date
and t2.re_date<=t1.end_date );
from A,B -- 此处笛卡尔乘积,表A的每一条记录与表B的每一条记录进行交叉
where -- 此处过滤所有符合条件的记录,去除不符合条件的记录
--这部分条件是去除时间眼中出范围的记录
to_date(re_date,'yyyy-mm-dd') between to_date(start_date,'yyyy-mm-dd') and to_date(end_date,'yyyy-mm-dd')
-- 这部分条件是去除人名不相等的记录
and name = tname
给表B中re_date赋值‘2011-9-16’,用re_date 去A表中判断它是否在时间范围内
to_date(re_date,'yyyy-mm-dd') between to_date(start_date,'yyyy-mm-dd') and to_date(end_date,'yyyy-mm-dd')
就是判断时间范围的条件啊我写了个例子,楼主你可以参照看下。
with a as (
select'xiaoa' name, to_date('2009-10-1','yyyy-mm-dd') start_date, to_date('2020-9-1','yyyy-mm-dd') end_date from dual union
select'xiaob', to_date('2010-3-2','yyyy-mm-dd'), to_date('2010-4-2','yyyy-mm-dd') from dual union
select'xiaoc', to_date('2011-11-2','yyyy-mm-dd'), to_date('2011-12-9','yyyy-mm-dd') from dual
),
b as (
select 'xiaoa' tname, to_date('2011-9-16','yyyy-mm-dd') re_date from dual
)
-- 上面部分是定义虚表,下面部分是实际的SQL语句
select name,start_date,end_date,tName,re_date
from a,b
where b.tname = a.name and
b.re_date between a.start_date and a.end_date;
例如 :select * from A where to_char(re_date,'yyyy-mm-dd')='2011-9-1'你写的就没有 to_char(re_date,'yyyy-mm-dd')='2011-9-1' 这个啊
估计是需求理解错了,按我的理解,re_date是表B中的一列,且re_date的值是在执行这次查询之前已经放入表B中,所以我使用了b.re_date between a.start_date and a.end_date来做约束条件。
如果不是这样的逻辑,请详细阐明。
with a as (
select'xiaoa' name, to_date('2009-10-1','yyyy-mm-dd') start_date, to_date('2020-9-1','yyyy-mm-dd') end_date from dual union
select'xiaob', to_date('2010-3-2','yyyy-mm-dd'), to_date('2010-4-2','yyyy-mm-dd') from dual union
select'xiaoc', to_date('2011-11-2','yyyy-mm-dd'), to_date('2011-12-9','yyyy-mm-dd') from dual
),
b as (
select 'xiaoa' tname, to_date('2011-9-16','yyyy-mm-dd') re_date from dual
)
-- 上面部分是定义虚表,下面部分是实际的SQL语句
select name,start_date,end_date,tName,re_date
from a,b
where b.tname = a.name and
b.re_date between a.start_date and a.end_date and
b.re_date = to_date('2011-9-16','yyyy-mm-dd') ;
假设数据库中re_date的值是 2011-9-16 12:01:52
那么 re_date = to_date('2011-9-16','yyyy-mm-dd')的结果为"假",
使用trunc()函数去除时分秒后再判断就可以了:
trunc(re_date) = to_date('2011-9-16','yyyy-mm-dd')的结果为"真",