简体   繁体   English

如果没有这样的字段而是实体,如何在 TypeORM 中的 SELECT 中进行子查询?

[英]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.我尝试在 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.但是“type_name”字段不在 ContactEntity 中。 How can I make a field from a subquery output with fields from the ContactEntity entity.如何使用来自 ContactEntity 实体的字段从子查询 output 创建一个字段。

From a where statement somewhere add something like this inside.从某处的 where 语句中添加类似这样的内容。

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

Cheers M8!干杯 M8!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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