How to query id not in condition with another table typeorm

Viewed 2276

Here is the query

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

Same query how to write using TypeORM typescript? Below is code, I tried which is not working

this.createQueryBuilder('tableA')
.where(`tableA.id != :id`, { id })
2 Answers
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();

This method will work fine for the above-mentioned problem.

You can select IDs from tableB first, and then use the IDs you've got (I assume that they are mapped and stored in var ids: string[]) to make a query for tableA.

this.createQueryBuilder('tableA')
  .where(`tableA.id <> ALL(:ids)`, { ids })
Related