日期:2014-05-18  浏览次数:20567 次

SQL 查询时间区间
表 table 

HTML code
表 table

id       日期      仓库      费用
1     2011-01-01    1         3
2     2011-01-02    1         2
3     2011-06-01    1         7
4     2011-02-01    2         12
5     2011-02-05    2         1


怎样查询日期在 2011-06-25 -- 2011-07-24 之间的数据,如果查不到, 就查询2011-05-25 -- 2011-06-24之间的数据,如果还查不到 就依次类推,知道查询到为止,


------解决方案--------------------
IF...ELSE

------解决方案--------------------

if exists(select 1 from tb where 日期 between '2011-06-25' and '2011-07-24')
begin
select * from tb where 日期 between '2011-06-25' and '2011-07-24'
end
else
if exists(select 1 from tb where 日期 between '2011-05-25' and '2011-06-24')
begin
select * from tb where 日期 between '2011-05-25' and '2011-06-24'
end