我有一张这样的桌子:
id timestart timestop
1 2014-5-5 23:15:00 2014-5-5 23:17:00
2 2014-5-5 23:30:00 2014-5-5 23:45:00
3 2014-5-5 23:50:00 2014-5-5 23:55:00
4 2014-5-5 23:57:00 2014-5-6 00:05:00我有select语句,它在timestart和timestop之间返回记录:
select *
from table
where ( timestart >= @start and timestop <= @stop)如果@start = '2014-5-5 23:00:00‘和@stop =’2014-5 23:59:00',则选择将返回前三行。我应该如何重写select语句以检索带有id=4的行(在timestart之后有@start,在timestop之后有@stop )?
通过这个声明,我得到了我想要的,但我不确定这是最好的方法:
... where
(timestart >= @start and timestart <= @stop) and (timestop <= @stop or timestop >= @stop) 发布于 2014-07-28 09:27:16
从第二个查询中可以很容易地推断出
where (timestart >= @start and timestart <= @stop) 甚至是
where (timestart BETWEEN @start and @stop) 这是因为你陈述的第二部分
where (timestart >= @start and timestart <= @stop)
and (timestop <= @stop or timestop >= @stop) <---this will be true for all timestop entries发布于 2014-07-28 09:35:33
试试这个:
where timestart BETWEEN ISNULL(@start,timestart) and ISNULL(@stop,timestart)https://stackoverflow.com/questions/24992084
复制相似问题