SQL INNER加入不给出结果

时间:2014-04-17 10:37:56

标签: php mysql sql

我有两个表格,信息如下:

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)

如何添加此加入/条件?我试过一个子查询但没有成功。

提前致谢!

4 个答案:

答案 0 :(得分:0)

您需要通过menu_dishes_status.dish_id对数据进行分组。然后Max(valid_to)可以正常工作

试试这个

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

让我知道这是否有效

答案 1 :(得分: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

答案 2 :(得分:0)

尝试这样,未经过测试,只是在我的头顶,尝试创建sql fiddle以获得更多帮助

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

答案 3 :(得分:0)

这是正确的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