简单库存 SQL 查询未列出所有产品 MS-ACCESS

Simple inventory SQL query not listing all products MS-ACCESS

所以我有这个简单的 SELECT 查询: design view of the query

SELECT tblProducts.product_ID, Sum(tblIntakes.intake_QTY)-Sum(tblExits.exit_QTY) AS Stock
FROM (tblProducts INNER JOIN tblExits ON tblProducts.product_ID = tblExits.product_ID) 
  INNER JOIN tblIntakes ON tblProducts.product_ID = tblIntakes.product_ID
GROUP BY tblProducts.product_ID;

它没有列出所有产品。它只列出那些有入口和出口的产品。这没有用,因为我想知道所有产品的库存水平。

这是我从查询中得到的:

| product_ID | Stock |
| 1          | 4     |

这是我不想得到的

| product_ID | Stock |
| 1          | 4     |
| 2          | 10    |
| 3          | 0     |

这应该不会太复杂,但我是新手并且 SQL 这让我很头疼。任何帮助将不胜感激

这些是记录:

产品

| product_ID | product_Name |
| 1          | Pencil       |
| 2          | Book         |
| 3          | Marker       |

摄入量

| intake_ID | intake_Date | product_ID  | intake_QTY |
| 1         | 20/07/2017  | 1           | 10         |
| 2         | 20/07/2017  | 2           | 10         |

退出

| exit_ID | exit_Date  | product_ID | exit_QTY |
| 1       | 21/07/2017 | 1          | 6        |

您正在寻找 left joinnz()

SELECT tblProducts.product_ID,
       nz(Sum(tblIntakes.intake_QTY), 0) - nz(Sum(tblExits.exit_QTY)) AS Stock
FROM (tblProducts LEFT JOIN
      tblExits
      ON tblProducts.product_ID = tblExits.product_ID
     ) LEFT JOIN
      tblIntakes
      ON tblProducts.product_ID = tblIntakes.product_ID
GROUP BY tblProducts.product_ID;