在酒店工作并编写了预订系统,每小时时间是无关紧要的
随着计费的进行。一切总是在晚上收费。 (除非您打算经营一个按小时收费的地方!;-)) 入住和退房是运营方面的考虑因素。
如果您真的想编写一个真正的预订系统,请不要使用存储过程。
它违背了拥有数据库的目的。
另外,写出这样的日期是 2007-04-29 真的很棒,因为不是每个人都来自同一个地方,这是一个国际标准。还要注意,如果你把它变成一个字符串,它仍然会被正确排序!
您需要创建一个日历表,因为 MySQL 没有内置函数来执行此操作。
此过程将为您建立日期。
drop table if exists calendar;
create table calendar
(
date_ date primary key
);
drop procedure fill_calendar;
delimiter $$
create procedure fill_calendar(start_date date, end_date date)
begin
declare date_ date;
set date_=start_date;
while date_ < end_date do
insert into calendar values(date_);
set date_ = adddate(date_, interval 1 day);
end while;
end $$
delimiter ;
call fill_calendar('2007-1-1', '2007-12-31');
来自:http://www.ehow.com/how_7571744_mysql-calendar-tutorial.html
drop table if exists rates;
create table rates
(
season varchar(100) primary key,
start_date date references calendar(date_),
end_date date references calendar(date_),
rate float
);
insert into rates values ('Low', '2007-01-01', '2007-04-30', 100.00);
insert into rates values ('High', '2007-05-01', '2007-08-31', 150.00);
insert into rates values ('Peak', '2007-09-01', '2007-12-21', 200.00);
select * from rates;
season start_date end_date rate
Low 2007-01-01 2007-04-30 100
High 2007-05-01 2007-08-31 150
Peak 2007-09-01 2007-12-21 200
我将忽略您在问题中给出的日期,并假设客户没有及时倒退。
select
date_, rate
from calendar
join rates
on date_ >= start_date and date_ <= end_date
where date_ between '2007-04-29' and '2007-5-01'
;
date_ rate
2007-04-29 100
2007-04-30 100
2007-05-01 150
select
sum(rate)
from calendar
join rates
on date_ >= start_date and date_ <= end_date
where date_ between '2007-04-29' and '2007-5-01'
sum(rate)
350
而且,正如您所见,该 sql 非常简洁易读,无需借助函数或过程。这将能够正确扩展并处理更复杂的问题。此外,由于数据是基于表的,因此可以使用引用检查。