一个sql日期段查询问题

langlg855 2007-06-27 03:37:05
数据库里有两个字段为日期:
客户 开始日期 结束日期 消费
205 2007-6-1 2007-6-15 50
205 2007-6-16 2007-6-20 20
205 2007-6-21 2007-6-25 10
现在需要查询日期段为2007-6-5至2007-6-19的纪录,得到前两条的纪录,句子应该怎么写呢?
...全文
441 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
肥胖的柠檬 2007-06-27
  • 打赏
  • 举报
回复

create table #r (客户 int, 开始日期 datetime, 结束日期 datetime )
insert into #r
select 205 , '2007-6-1' , '2007-6-15' union all
select 205 , '2007-6-16', '2007-6-20' union all
select 205 , '2007-6-21' , '2007-6-25'

select * from #r where 开始日期 between '2007-6-5' and '2007-6-19' or 结束日期 between '2007-6-5' and '2007-6-19'

yrwx001 2007-06-27
  • 打赏
  • 举报
回复
declare @stardate varchar(10),@enddate varchar(10)
select * from
(
select top 1 * from table where 开始日期 <= @stardate order by 开始日期 desc
union all
select top 1 * from table where 开始日期 <= @enddate order by 开始日期 desc
union all
select * from table table where 开始日期 between @stardate and @enddate
)A
order by A.開始時間
昵称被占用了 2007-06-27
  • 打赏
  • 举报
回复
select * from tablename
where 结束日期>='2007-6-5'
and 开始日期<='2007-6-19'

34,590

社区成员

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

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