Hotel Booking Rates SQL Problem

后端 未结 3 557
清歌不尽
清歌不尽 2021-01-16 07:33

Hi I\'ve been trying to get my own system for checking rooms rates going after reading other questions here on StackOverflow.

My query works fine and produces corre

3条回答
  •  粉色の甜心
    2021-01-16 07:54

    Thanks for your answers, Martin your produced 2 rows but no days...Emilio your answer got me thinking about how I had set up the rates. I changed the rate table to date format instead of datetime and made the rate_end_date the same day as the next rate_start_date.

    0   66  2011-01-01  2011-04-15
    1   70  2011-04-15  2011-06-01
    2   80  2011-06-01  2011-07-01
    3   100 2011-07-01  2011-09-01
    4   80  2011-09-01  2011-10-01
    5   70  2011-10-01  2011-11-01
    6   45  2011-11-01  2012-01-01
    

    then dropped the +1 and

    SELECT rates.rate_id, rate_start_date, rate_end_date, rate_price, 
    (DATEDIFF( IF (rate_end_date > '2011-04-16' , '2011-04-16', rate_end_date), 
    IF ( rate_start_date < '2011-04-14' , '2011-04-14' , rate_start_date )) ) 
    AS days FROM rates WHERE rate_start_date <= '2011-04-16' 
    AND rate_end_date > '2011-04-14' 
    ORDER BY rate_price ASC
    

    produced

    rate_id   rate_start_date   rate_end_date   rate  days    
        0   2011-01-01  2011-04-15  66  1
        1   2011-04-15  2011-06-01  70  1
    

    and a query with no overlapping rates from the 1st to the 8th of April:

    SELECT rates.rate_id, rate_start_date, rate_end_date, rate_price, 
    (DATEDIFF( IF (rate_end_date > '2011-04-08' , '2011-04-08', rate_end_date), 
    IF ( rate_start_date < '2011-04-01' , '2011-04-01' , rate_start_date )) ) 
    AS days FROM rates WHERE rate_start_date <= '2011-04-08' 
    AND rate_end_date > '2011-04-01' 
    ORDER BY rate_price ASC
    

    produces:

    rate_id   rate_start_date    rate_end_date   rate  days
        0     2011-01-01       2011-04-15     66    7
    

    thanks agiain for your help!

提交回复
热议问题