简体   繁体   中英

How to make a subquery in SELECT in TypeORM if there is no such field but an entity?

I try create query with subquery in TypeORM.

    const query = await this.contactRepository.createQueryBuilder('c')
      .select(['c.id', 'c.type_id', 'c.value'])
      .addSelect(subQuery => {
        return subQuery
        .select('system_name', 'type_name')
        .from(ContactTypePropsEntity, 'ctp')
        .where('ctp.id = c.type_id')
      }, 'type_name')
      .where('c.entity_id = :entity_id AND c.entity_type = :entity_type AND c.deleted = :deleted', {
        entity_id: id,
        entity_type: type,
        deleted: 0
      })
      .getMany();

But the field 'type_name' is not in ContactEntity. How can I make a field from a subquery output with fields from the ContactEntity entity.

From a where statement somewhere add something like this inside.

(qb: SelectQueryBuilder<SomeEntity>) => {
    qb.where(`parentQueryEntity.id in ` +
        qb.subQuery()
            .select('s.id')
            .from(SomeEntity, 's')
            .getQuery(),
    )
}

Cheers M8!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM