求某一时间段是否在另一个时间段内的SQL语句

luojingjing 2011-04-04 02:21:46
比如:
id StartDate EndDate
1 2011-03-01 2011-03-15
2 2011-03-08 2011-03-09
3 2011-04-02 2011-04-20

搜索2011/03/08 - 2011/03/09时,ID为1的数据出来
搜索2011/03/01 - 2011/03/15时,ID为2的数据出来


即求某一时间段是否在另一个时间段内的SQL语句

...全文
1095 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
luojingjing 2011-04-04
  • 打赏
  • 举报
回复
select * from tb where ('2011-03-08' between StartDate and EndDate) or ('2011-03-10' between StartDate and EndDate )

union
select * from tb where StartDate>='2011-03-08' and EndDate<='2011-03-10'



select * from tb where ('2011-03-01' between StartDate and EndDate) or ('2011-03-15' between StartDate and EndDate )

union
select * from tb where StartDate>='2011-03-01' and EndDate<='2011-03-15'



时间的交集,自己写出来了,但不知道还有没有遗漏.

请高手帮忙看看.
喜-喜 2011-04-04
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 luojingjing 的回复:]
我想要的是一条SQL语句喔..
id=1 and StartDate<'2011-03-08' and EndDate>'2011-03-09'
id=2 and StartDate>='2011-03-01' and EndDate<='2011-03-15'

符号的不同方向没法用在同一条SQL吧?
[/Quote]
拿你没办法,我先闪了!呵呵...
luojingjing 2011-04-04
  • 打赏
  • 举报
回复
就是给一个时间段,是否有跟数据库存在的日期相交?找到相交的记录

luojingjing 2011-04-04
  • 打赏
  • 举报
回复
我想要的是一条SQL语句喔..
id=1 and StartDate<'2011-03-08' and EndDate>'2011-03-09'
id=2 and StartDate>='2011-03-01' and EndDate<='2011-03-15'

符号的不同方向没法用在同一条SQL吧?





mabailin 2011-04-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 happycell188 的回复:]

SQL code
where id=2 and StartDate>='2011-03-01' and EndDate<='2011-03-15'
--这只不过就是比较符号的方向问题
[/Quote]

同意,但是你的数据库要保证StartDate<=EndDate
喜-喜 2011-04-04
  • 打赏
  • 举报
回复
where id=2 and StartDate>='2011-03-01' and EndDate<='2011-03-15'
--这只不过就是比较符号的方向问题
AcHerat 元老 2011-04-04
  • 打赏
  • 举报
回复
你的搜索条件和你数据里的起始日期到底是什么关系,要说明下吧!
So_CooL 2011-04-04
  • 打赏
  • 举报
回复
select id,StarDate,EndDate from
tb where id = 1 and StarDate between '2011/03/08' and '2011/03/09'

select id,StarDate,EndDate from
tb where id = 2 and StarDate between '2011/03/01' and '2011/03/15'
luojingjing 2011-04-04
  • 打赏
  • 举报
回复
就是不知道怎么写啊?请问要怎么修改啊?
搜索'2011-03-01','2011-03-15'时,'2011-03-08','2011-03-09'的记录也要出来
喜-喜 2011-04-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 luojingjing 的回复:]
楼上的,你好,因为'2011-03-08','2011-03-09'在'2011-03-01','2011-03-15'里面,所以搜索'2011-03-01','2011-03-15'时,'2011-03-08','2011-03-09'的记录也要出来.

你写的方法没有出来呢.
[/Quote]

哦,可能是我理解错了!写法就是上面的写法,你具体想要什么样的结果,可以自己修改一下...
luojingjing 2011-04-04
  • 打赏
  • 举报
回复
楼上的,你好,因为'2011-03-08','2011-03-09'在'2011-03-01','2011-03-15'里面,所以搜索'2011-03-01','2011-03-15'时,'2011-03-08','2011-03-09'的记录也要出来.

你写的方法没有出来呢.
喜-喜 2011-04-04
  • 打赏
  • 举报
回复
use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
id int,
StartDate datetime,
EndDate datetime
)
go
--插入测试数据
insert into tb select 1,'2011-03-01','2011-03-15'
union all select 2,'2011-03-08','2011-03-09'
union all select 3,'2011-04-02','2011-04-20'
union all select 1,'2011-03-01','2011-03-23'
union all select 2,'2011-02-23','2011-03-18'
go
--代码实现

select id,StartDate=convert(varchar(10),StartDate,120)
,EndDate=convert(varchar(10),EndDate,120)
from tb where id=1 and StartDate<'2011-03-08' and EndDate>'2011-03-09'

/*测试结果

id StartDate EndDate
-------------------------------
1 2011-03-01 2011-03-15
1 2011-03-01 2011-03-23

(2 行受影响)
*/

select id,StartDate=convert(varchar(10),StartDate,120)
,EndDate=convert(varchar(10),EndDate,120)
from tb where id=2 and StartDate<'2011-03-01' and EndDate>'2011-03-15'

/*测试结果

id StartDate EndDate
-------------------------------
2 2011-02-23 2011-03-18

(1 行受影响)
*/

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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