MS Access 2013:为什么没有匹配项时我的 RIGHT JOIN 查询不返回 NULL?

MS Access 2013: Why is my RIGHT JOIN query not returning a NULL when there is no match?

我有一个带有 ID 字段(自动编号)和 item 字段的自定义 table。我公司提供的另一个 table 有很多字段,但它也有一个 itemwhseqty_on_hand 字段。

我只想查找自定义 table 中列出的项目,所以我使用了 RIGHT JOIN。 但是,我还需要根据某些条件进行过滤。我需要 'whse = A' 和 'qty_on_hand > 0',但是当我这样做时,它会排除不匹配的项目,而不仅仅是 return 一个 NULL。我怎样才能使自定义 table 中的所有行都被 return 编辑,如果没有匹配项,则 return NULL?

我主要使用 Access 中的设计视图,但这是我在设计视图中创建的SQL:

SELECT 
     customtable.ID
     ,customtable.item
     ,Sum(companytable.qty_on_hand) AS SumOfqty_on_hand
     ,companytable.whse
FROM companytable 
     RIGHT JOIN customtable ON companytable.item = customtable.item
GROUP BY 
     customtable.ID
     ,customtable.item
     ,companytable.whse
HAVING 
     (((Sum(companytable.qty_on_hand))>0) 
     AND ((companytable.whse)="A"))
ORDER BY 
     customtable.ID;

您可以将其切换为 LEFT JOIN,以便您可以对 ON 子句中的 companytable 应用一些过滤,这会导致您的 companytable 在加入之前删除:

SELECT customtable.ID,
    customtable.item,
    Sum(companytable.qty_on_hand) AS SumOfqty_on_hand,
    companytable.whse
FROM customtable
LEFT JOIN companytable ON 
    companytable.item = customtable.item AND
    companyTable.qty_on_hand > 0 AND
    companyTable.whse = "A"
GROUP BY customtable.ID,
    customtable.item,
    companytable.whse   
ORDER BY customtable.ID;

我不确定 Access 如何在其 "design" 视图中表示它,但它应该可以正常工作。

或者,您可以使用在加入之前过滤 companytable 的子查询:

SELECT customtable.ID,
    customtable.item,
    Sum(comptable.qty_on_hand) AS SumOfqty_on_hand,
    comptable.whse
FROM (SELECT * FROM companytable WHERE qty_on_hand > 0 AND whse = "A") AS comptable
RIGHT JOIN customtable ON comptable.item = customtable.item
GROUP BY customtable.ID,
    customtable.item,
    comptable.whse
ORDER BY customtable.ID;