Mysql日期范围价格

时间:2016-02-27 16:46:14

标签: mysql sql database date

我想根据季节日期的愤怒来显示价格。

示例:

  

电影具有根据日期确定的不同价格

season_1 = 2016-05-01 - 2016-08-31价格为100 $
season_2 = 2016-09-01 - 2017-04-31价格为50美元

我在表格中有两个日期

season_tbl:

id    |   start_date   |  end_date   |  name
--------------------------------------------------
1     |  2016-05-01    | 2016-08-31  | season_1
2     |  2016-09-01    | 2017-04-30  | season_2

movies_price_tbl:

id    |   season_id    |  name            |  price | movie
------------------------------------------------------------
1     |  1             | Gladiator movie  | 100$   |  1
2     |  2             | Gladiator movie  | 50%    |  1

我的期望:

当用户输入搜索开始2016-06-01结束2016-07-30并选择该日期的电影显示价格ID时。在这种情况下,显示season_1价格100$。 如果用户输入开始2016-09-23结束2017-01-21显示season_2价格

我尝试但不工作:

WHERE m.movie_id = 1 AND s.start_date <= '2016-05-01' AND s.end_date >= '2016-07-20'

2 个答案:

答案 0 :(得分:0)

也许这个查询会这样做:

SELECT movies_price_tbl.price FROM season_tbl 
  INNER JOIN movies_price_tbl ON season_tbl.id = movies_price_tbl.season_id 
    WHERE movies_price_tbl.movie = '1' 
    AND season_tbl.start_date <= '2016-05-01' 
    AND season_tbl.end_date >= '2016-07-20'

PS:你应该摆脱name中的movies_price_tbl - 列。没有必要,因为有movie_id

答案 1 :(得分:0)

尝试此查询

SELECT price FROM movies_price_tbl WHERE movie_id = 1 && season_id = (select id from season_tbl where start_date <= '2016-05-01' && end_date >= '2016-07-20')