谁帮写个sql语句

来源:百度知道 编辑:UC知道 时间:2024/06/14 09:14:02
现在数据库的数据如下:

(ID) (房型) (平时价格) (开始日期) (结束日期) (新价格)
ID Name Price Date1 Date2 NewPrice
1 标房 200 null null null
2 豪房 250 09.10.14 09.10.19 500
2 豪房 250 09.10.20 09.10.28 350

就是说一个房型如没有注明哪段时间的新价格就按平时价格执行

现在假如要选择2009-10-13日价格<300但>190的房型,显然上面的标房和豪房都符合要求(13日没有指明新价格所以按平时价格);

现在假如要选择2009-10-18日价格<300但>190的房型,显然上面的标房符合,而豪华房不符合;

现在假如要选择2009-10-21日价格<400但大于300的房型,显然上面的标房不符合,而豪华房符合;

这个sql语句怎么写?假如现在要选择某天(date)的价格小于400但大于350的房型?

sql=select * from Table where ....<400 and ...>300

请帮补充完整并且对于任何一天都适合。谢谢!

select * From (
select id,name,case when to_date('20091015','yyyymmdd') between date1 and date2 then newprice else price end as pr
from room) t where t.pr between 200 and 300

看看行不

select * from Table where Price<400 and Price>300
and Date1='2009-6-2'

首先建议楼主数据库最好不要null填充,如果是已有数据的话不方便该也就算了
sql=(select * from Table where price<400 and price>300 and date1 not like '%')uion(select * from Table where date1<YOUR_DAY and date2>YOUR_DAY and NewPrice<400 and NewPrice>300 )

前一半not like '%'用来区分是不是null的,(当然最好是用数值0代替null然后看看是不是0好一些)
YOUR_DAY是你给出的日期,和date1,date2一样应该是日期格式

什么数据库,还有你说的 任何一天 是不是只 当天?

declare @date datetime
set @date = '2009.07.01'--时间
select distinct a.id,a.name,isnull(b.NewPrice,a.Price) trueprice
from [table] a
left join
(select id,name,NewPrice,date1
from [table]
where datediff(d,date1,@date)>-1