2016-02-27 123 views
0

我想根據季節日期風格顯示價格。Mysql日期範圍價格

例子:

電影有不同的價格是基於日期確定

season_1 = 2016年5月1日 - 2016年8月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' 
+0

這是數據類型的列START_DATE日期和結束日期? – scaisEdge

+0

列的類型首先是'日期',我將其更改爲'varchar' – Ivan

+0

現在在db中是varchar? ..你的查詢得到了什麼結果?顯示您使用的所有查詢代碼.. – scaisEdge

回答

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

+0

這個例子工作,但一個不工作當我輸入開始日期'2016-01-01'到結束日期'2016-02-01'返回'null'。但範圍內的所有其他工作。 – Ivan

+0

還有什麼應該退回 - 2016-01-01和2016-02-01之間沒有季節限定 – Olli

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')