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

创建了一个油价表如何查询出其中的数据
create table OilPrice(startYear int,startMonth int,startDay int,endYear int,endMonth int,endDay int,oilprice float)
其中各列代表的含义是:
startYear开始年
startMonth开始月
startDay开始日

endYear结束年
endMonth结束月
endDay结束日

我想从中查询出2012年3月22日的油价,应该怎么求出(表中的数据是一段一段的,也就是2012年3月22日在一段时间里),请问各位大牛,如何查处数据,谢谢!




------解决方案--------------------
select * from OilPrice where cast('2012-03-22' as datetime) between
cast(cast(startYear as varchar) + '-' + cast(startMonth as varchar) + '-' + cast(startDay as varchar) as datetime) and
cast(cast(endYear as varchar) + '-' + cast(endMonth as varchar) + '-' + cast(endDay as varchar) as datetime)
------解决方案--------------------
SQL code
create table OilPrice(startYear int,startMonth int,startDay int,endYear int,endMonth int,endDay int,oilprice float)
insert into OilPrice values(2012,1,1,2012,2,1,1)
insert into OilPrice values(2012,2,1,2012,3,1,2)
insert into OilPrice values(2012,3,1,2012,4,1,3)
insert into OilPrice values(2012,4,1,2012,5,1,4)
insert into OilPrice values(2012,5,1,2012,6,1,5)
insert into OilPrice values(2012,6,1,2012,7,1,6)
go

select * from oilprice
/*
startYear   startMonth  startDay    endYear     endMonth    endDay      oilprice                                              
----------- ----------- ----------- ----------- ----------- ----------- ----------------------------------------------------- 
2012        1           1           2012        2           1           1.0
2012        2           1           2012        3           1           2.0
2012        3           1           2012        4           1           3.0
2012        4           1           2012        5           1           4.0
2012        5           1           2012        6           1           5.0
2012        6           1           2012        7           1           6.0

(所影响的行数为 6 行)
*/

select * from OilPrice where cast('2012-03-22' as datetime) between
cast(cast(startYear as varchar) + '-' + cast(startMonth as varchar) + '-' + cast(startDay as varchar) as datetime) and
cast(cast(endYear as varchar) + '-' + cast(endMonth as varchar) + '-' + cast(endDay as varchar) as datetime) 
/*
startYear   startMonth  startDay    endYear     endMonth    endDay      oilprice                                              
----------- ----------- ----------- ----------- ----------- ----------- ----------------------------------------------------- 
2012        3           1           2012        4           1           3.0

(所影响的行数为 1 行)
*/


drop table oilprice

------解决方案--------------------
探讨

SQL code
create table OilPrice(startYear int,startMonth int,startDay int,endYear int,endMonth int,endDay int,oilprice float)
insert into OilPrice values(2012,1,1,2012,2,1,1)
insert into OilPrice val……