繁体   English   中英

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字段的自定义表。 我的公司提供了另一个表,其中包含许多字段,但是它还具有itemwhseqty_on_hand字段。

我只想查找自定义表中列出的项目,因此我使用了RIGHT JOIN。 但是 ,我还需要过滤一些条件。 我需要'whse = A'和'qty_on_hand> 0',但是当我这样做时,它将排除没有匹配项的项目,而不仅仅是返回NULL 我该如何做,以便返回自定义表中的所有行,如果不匹配,则返回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 ,这样就可以应用一些过滤到companytable您在ON子句这将导致你的那些记录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将如何在“设计”视图中表示这一点,但是应该可以正常工作。

或者,您可以使用子查询在加入之前过滤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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM