首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL 查询时间区间解决方法

2012-04-28 
SQL 查询时间区间表tableHTML code表 tableid日期仓库费用12011-01-011322011-01-021232011-06-011742011-

SQL 查询时间区间
表 table 

HTML code
表 tableid       日期      仓库      费用1     2011-01-01    1         32     2011-01-02    1         23     2011-06-01    1         74     2011-02-01    2         125     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

热点排行
Bad Request.