SELECT INNER JOIN(或其他)结果的设置值不在第二个 table

SELECT with INNER JOIN (or other) setting value for result that is not in the second table

我不知道如何用 SQL (Firebird 2.5) 做到这一点。

我有两个 table。在第一个 table 中,我有一个包含产品信息的列表,在第二个 table 中,我有使用主键关系的订单,第一个 table 的产品。

我需要 select 产品中的所有项目 table 并取订单中使用的产品的全部数量的总和 table。如果该产品未在任何订单中使用,我仍然需要列出的产品,总和为零。

您需要左连接和合并,例如:

 select table1.product , coalesce(sum(table2.quantity) ,0)
 from table1 
 left  join  table2 on table1.col1key = table2.col2key
 group by table1.product