Search code examples
mysqlnode.jstypescriptnestjstypeorm

How to query id not in condition with another table typeorm


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 })

Solution

  • 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.