2014-04-17 215 views
2

我有信息兩個表如下:SQL INNER JOIN不給結果

Table: menu_dishes 
dish_id dish_title  ingredients 
DSH01  Bacon & Eggs Bacon, eggs 

Table: menu_dishes_status 
dish_id valid_from valid_to  price 
DSH01 2013-10-10 2013-12-31 10.00 
DSH01 2014-01-01 2014-02-28 12.00 

第一個表中包含的菜,第二個,包含菜狀態。所以一盤菜的當前狀態是CURRENT_DATE在valid_from和valid_to之間的日期,而退出的菜是MAX(valid_to)< CURRENT_DATE。

我參加這些表和分類數據:

'n' NEW => WHERE CURRENT_DATE BETWEEN valid_from AND (valid_from + 1 MOTNH) 
'c' CURRENT => WHERE CURRENT_DATE > (valid_from + 1 MOTNH) 
'r' RETIRED => the rest of the rows, so the "dish_id" items not in the tables above, BUT 
      returning the values from the row containing MAX(valid_to) date. 

查詢(從論壇幫助)是:

SELECT 
    menu_dishes.dish_id, 
    menu_dishes.dish_title, 
    menu_dishes.ingredients, 
    menu_dishes_status.price, 
CASE 
    WHEN 
     CURRENT_DATE BETWEEN menu_table_status.valid_from 
         AND DATE_ADD(menu_table_status.valid_from, INTERVAL 1 MONTH) 
     AND CURRENT_DATE < menu_table_status.valid_to 
    THEN 'n' 
    WHEN 
     CURRENT_DATE > DATE_ADD(menu_table_status.valid_from_date, INTERVAL 1 MONTH) 
     AND CURRENT_DATE < menu_table_status.valid_to 
    THEN 'c' 
    ELSE 'r' 
END as status 
FROM menu_dishes 
INNER JOIN 
    menu_dishes_status ON (menu_dishes.dish_id = menu_dishes_status.dish_id) 
ORDER BY menu_dishes.dish_title ASC 

以上的回報(在DSH01數據例子的情況下, )兩行,一行$ 10,一行$ 12。我需要'r'項來包含最近一段時間的價格,或者MAX(valid_to)

如何添加此連接/條件?我嘗試了一個沒有成功的子查詢。

在此先感謝!

回答

0

創建sql fiddle下面是正確的SQL。謝謝大家的幫助:

SELECT 
    menu_dishes.dish_id, 
    menu_dishes.dish_title, 
    menu_dishes.ingredients, 
    menu_dishes_status.price, 
CASE 
    WHEN 
     CURRENT_DATE BETWEEN menu_table_status.valid_from 
         AND DATE_ADD(menu_table_status.valid_from, INTERVAL 1 MONTH) 
     AND CURRENT_DATE <= menu_table_status.valid_to 
    THEN 'n' 
    WHEN 
     CURRENT_DATE > DATE_ADD(menu_table_status.valid_from_date, INTERVAL 1 MONTH) 
     AND CURRENT_DATE <= menu_table_status.valid_to 
    THEN 'c' 
    ELSE 'r' 
END as status 
FROM menu_dishes 
INNER JOIN 
    menu_dishes_status ON (menu_dishes.dish_id = menu_dishes_status.dish_id) 
WHERE menu_dishes_status.valid_to_date = (SELECT MAX(intab.valid_to_date) 
              FROM menu_dishes_status AS intab 
              WHERE intab.dish_id = menu_dishes_status.dish_id) 
ORDER BY menu_dishes.dish_title ASC 
0

您將需要通過menu_dishes_status.dish_id擁有組數據。馬克斯(失效日期),然後將很好的工作

試試這個

FROM menu_dishes 
INNER JOIN 
    menu_dishes_status ON (menu_dishes.dish_id = menu_dishes_status.dish_id) 
group by menu_dishes_status.dish_id having MAX(valid_to) 
ORDER BY menu_dishes.dish_title ASC 

讓我知道這是否正常工作

+0

快樂它幫助:) –

+0

嗨user2629673,抱歉地說,在進一步測試你的建議是不是爲我工作。當dish_id在menu_dishes_status中有一行「歷史記錄」時,查詢返回價格的第二個最近(不是當前)值,並強制這些dish_id行被分類爲'r' – TheRealPapa

+0

在上面的示例數據中,它是否返回2013-12-31,10.00 for DSH01?如果您可以向我發送數據腳本以及您想要輸出的內容,這將是一件好事 - 然後我可以向您發送一個確切的查詢,以便使用。 –

0

試試這個,它可能會有所幫助。

CASE 

WHEN ((CURRENT_DATE BETWEEN menu_table_status.valid_from AND 
     DATE_ADD(menu_table_status.valid_from, INTERVAL 1 MONTH)) 
     AND CURRENT_DATE < menu_table_status.valid_to) 
THEN 'n' 

WHEN ((CURRENT_DATE > DATE_ADD(menu_table_status.valid_from_date, INTERVAL 1 MONTH)) 
     AND CURRENT_DATE < menu_table_status.valid_to) 
THEN 'c' 

ELSE 'r' 

END as status 
0

試試這個樣子,沒有測試過,就在我的頭頂,爭取更多的幫助

SELECT 
    menu_dishes.dish_id, 
    menu_dishes.dish_title, 
    menu_dishes.ingredients, 
    menu_dishes_status.price, 
CASE 
    WHEN 
     CURRENT_DATE BETWEEN menu_table_status.valid_from 
         AND DATE_ADD(menu_table_status.valid_from, INTERVAL 1 MONTH) 
     AND CURRENT_DATE < menu_table_status.valid_to 
    THEN 'n' 
    WHEN 
     CURRENT_DATE > DATE_ADD(menu_table_status.valid_from_date, INTERVAL 1 MONTH) 
     AND CURRENT_DATE < menu_table_status.valid_to 
    THEN 'c' 
    ELSE 'r' 
END as status 
FROM menu_dishes 
INNER JOIN 
menu_dishes_status ON (menu_dishes.dish_id = menu_dishes_status.dish_id) 
WHERE menu_dishes_status.valid_to=(SELECT MAX(valid_to) FROM menu_dishes_status WHERE menu_dishes_status.dish_id=menu_dishes.dish_id) 
ORDER BY menu_dishes.dish_title ASC