如何查询不符合另一个 table typeorm 条件的 id

How to query id not in condition with another table typeorm

这里是查询

SELECT * from tableA WHERE tableA.id NOT IN (SELECT tableB.a_id FROM tableB);

同样的查询如何使用TypeORM打字稿来写? 下面是代码,我试过是行不通的

this.createQueryBuilder('tableA')
.where(`tableA.id != :id`, { id })

您可以先从 tableB 获取 select ID,然后使用您获得的 ID(我假设它们已映射并存储在 var ids: string[] 中)进行查询tableA.

this.createQueryBuilder('tableA')
  .where(`tableA.id <> ALL(:ids)`, { ids })
const tableBqry = tableBRepository
       .createQueryBuilder('tableB')
       .select("tableb_id");

const tableAqry = tableARepository
       .createQueryBuilder('tableA')
       .where("tableA.id NOT IN (" + tableBqry.getSql() + ")");

const results = await tableAqry.getMany();

此方法可以很好地解决上述问题。