table:id startdate enddate field
1 2007-1-1 2007-1-1 aaf
2 2007-1-2 2007-1-3 ads
3 2007-1-13 2007-1-14 afds
4 2007-1-14 2007-1-14 asfss
5 2007-1-26 2007-1-27 adfsdfx
....查询2007-1-1到2007-1-3之间的数据:
得到如下记录:
1 2007-1-1 2007-1-1 aaf
2 2007-1-2 2007-1-3 ads查询2007-1-2到2007-1-5之间的数据:
得到如下记录:
2 2007-1-2 2007-1-3 ads-------就是怎么实现时间段查询啊,有两个日期在的时候..
1 2007-1-1 2007-1-1 aaf
2 2007-1-2 2007-1-3 ads
3 2007-1-13 2007-1-14 afds
4 2007-1-14 2007-1-14 asfss
5 2007-1-26 2007-1-27 adfsdfx
....查询2007-1-1到2007-1-3之间的数据:
得到如下记录:
1 2007-1-1 2007-1-1 aaf
2 2007-1-2 2007-1-3 ads查询2007-1-2到2007-1-5之间的数据:
得到如下记录:
2 2007-1-2 2007-1-3 ads-------就是怎么实现时间段查询啊,有两个日期在的时候..
select * from tablename where startdate >= '2007-1-1' and enddate <= '2007-1-3'查询2007-1-2到2007-1-5之间的数据:
select * from tablename where startdate >= '2007-1-2' and enddate <= '2007-1-5'
比如现在的
2007-1-2 2007-1-3 ads
我原来的设计是
2007-1-2 ads
2007-1-3 ads
...但老大(不懂技术这一块的),说两个不好..让人错误以为是两天不一样的(虽然实质上不是)所以我想改成我上面的...但这样一来,如果查询2007-1-3到2007-1-13之间的数据就查不到....
我原先的方法就可以查到(虽然只能查询到其中一天的记录)...
我不知道该使用哪个方法好点...
照理说,后者,没有数据冗余....
楼主想要的就是这样的语句吧?
查询2007-1-3到2007-1-13 你想要得到什么样的结果呢?像下面的么
2 2007-1-2 2007-1-3 ads
3 2007-1-13 2007-1-14 afds
4 2007-1-14 2007-1-14 asfss
3 2007-1-13 2007-1-14 afdsselect * from tablename where startdate >= l_ksrq and enddate <= l_jsrq 这个SQL语句是得不到的哦!
select * from tablename where startdate >= '2007-1-2' and enddate <= '2007-1-5'
| |
你要查的开始时间 你要查的结束时间
3 2007-1-13 2007-1-14 afdsselect * from tablename where startdate >= l_ksrq and enddate <= l_jsrq 这个SQL语句是得不到的哦!==================================晕,那为什么不要
4 2007-1-14 2007-1-14 asfss
这条纪录呢如果你的需求是这样的话
这样设计就有点问题了
这个语句可以么
查询2007-1-3到2007-1-13时候,按照请笛你的那条SQL语句,就得不到如下数据 :
2007-1-13 2007-1-14 afds虽然日期段只包括这两天中1月13日这一天,但也希望显示出来...
select * from tablename where (startdate between l_ksrq and l_jsrq) or (enddate between l_ksrq and l_jsrq)
begin
drop table t_test
end
create table t_test
(
id int ,
startdate datetime ,
enddate datetime ,
field varchar(20)
)insert into t_Test
select 1 , '2007-1-1', '2007-1-1', 'aaf' union
select 2 , '2007-1-2', '2007-1-3', 'ads'select *
from t_Test
where startdate >= @start_date
and enddate <= @end_date
select *
from t_test
where ( startdate between @start_date and @end_date )
or ( enddate between @start_date and @end_date )