SQL 查询 Table 1 与外键引用的 Table2 子项之和的差异

SQL Query Difference between Table 1 compared to Sum of Table2 Child Items Referenced by Foreign Key

假设我有 2 个表:

Table1

 ID    Name    Quantity
 1     Pencil        12
 2     Pen           35
 3     Ruler         50

Table 2

 ID    Name    Quantity   FK_Table1
 1     Pencil         6           1
 2     Pencil         6           1
 3     Pen           10           2
 4     Pen            5           2
 5     Pen            5           2
 6     Pen            5           2
 7     Ruler         12           3
 8     Ruler         12           3
 9     Ruler         12           3
10     Ruler         12           3

我该怎么做?

尝试以下查询:

select * from table1 t1
where t1.Quantity <> (select sum(t2.Quantity) from table2 t2 where t2.FK_Table1 = t1.ID);

用于获取 Table 2 的记录:

select * from table2 where FK_Table1 IN(
  select t1.ID from table1 t1
  where t1.Quantity <> (
    select sum(t2.Quantity) from table2 t2 where t2.FK_Table1 = t1.ID
   )
);