1

MDW_CUSTOMER_ACCOUNTS具有以下字段:ACCOUNT_ID, MEAL_ID.

MDW_MEALS_MENU具有以下字段:MEAL_ID, MEAL_NAME.

我正在尝试生成有关客户使用查询订阅特定餐点的次数的报告,

SELECT count(a.account_id), b.meal_id, b.meal_name
    FROM mdw_meals_menu b LEFT JOIN mdw_customer_accounts a
    on b.meal_id=a.meal_id
    WHERE
    a.start_date BETWEEN to_date('01-APR-2013','DD-MON-YYYY')
    AND to_date('30-JUN-2013','DD-MON-YYYY')
    GROUP BY b.meal_id, b.meal_name
    ORDER BY count(a.account_id) desc, b.meal_id;

这只会列出MEAL_ID至少订阅过一次的 s。但它不显示尚未订阅的 Id。

如何让这些MEAL_IDs 在 count 为 的情况下打印0

我已经修改了代码,但我仍然得到相同的结果。

4

2 回答 2

4

您的 where 子句有效地将您的外部联接转换回内部联接 - 外部联接表上的条件通常应该在联接子句中,如下所示:

SELECT count(a.account_id), b.meal_id, b.meal_name
FROM mdw_meals_menu b 
LEFT JOIN mdw_customer_accounts a
       on b.meal_id=a.meal_id and
          a.start_date BETWEEN to_date('01-APR-2013','DD-MON-YYYY')
                           AND to_date('30-JUN-2013','DD-MON-YYYY')
GROUP BY b.meal_id, b.meal_name
ORDER BY count(a.account_id) desc, b.meal_id;
于 2013-06-11T08:09:54.293 回答
1

您应该使用左外连接。

于 2013-06-11T08:07:31.793 回答