SQL 查询以显示另一个 table 中丢失的记录

SQL query to show missing records from another table

我有一份客户名单table。第二个 table 包含事件。 如果事件 table 中缺少名称,它应该从具有“0”值的 customers.name table 中获取它。

客户table:

|   NAME    |  ADDRESS  |
+-----------+-----------+
|   Chris   |    XXX    |
|   Tom     |    YYY    |
|   John    |    ZZZ    |

事件Table:

   NAME   |   QUANTITY   |
----------+--------------+
  Chris   |       2      |
  Tom     |       4      |

如果事件 table 中缺少一个名字,查询应该从客户 table 中添加它。 (仅供展示,不应添加到记录中)。

结果应该是这样的:

   NAME   |   QUANTITY   |
----------+--------------+
  Chris   |       2      |
  Tom     |       4      |
  John    |       0      |

我正在使用 Firebird 2.5

您可以使用左联接

select a.name, coalesce(b.quantity, 0) as  quantity
from Customers  a 
left join Events  b on a.name = b.name 

这就是 left join 的作用:

select c.name, coalesce(e.quantity, 0) as quantity
from customers c left join
     events e
     on c.name = e.name;

或为空

select a.name, isnull(b.quantity, 0) quantity
from Customers  a 
left join Events  b on a.name = b.name