簡體   English   中英

我如何在 `typeorm` 中使用 `postgresql` select `array_agg`?

[英]How can I select `array_agg` in `typeorm` with `postgresql`?

我在 typescript 項目中使用typeorm連接到postresql 11 我在 sql 中有以下查詢,它給了我預期的結果:

select "customerUuid", array_agg("siteUuid") from "SiteCustomer" where "customerUuid" in ('id1', 'id2') group by "customerUuid";

但是當我使用typeorm時,它總是返回空數組:

     queryRunner.manager.getRepository(SiteCustomer).
          .createQueryBuilder()
          .select('"customerUuid", array_agg("siteUuid")')
          .where('"customerUuid" IN (:customerUuids)', { customerUuids: customerUuids})
          .groupBy('"customerUuid"')
          .getMany();

如何讓typeorm在 postgresql 中與array_agg一起工作?

你可以做這樣的事情 -

      let baseQuery = queryRunner.manager.getRepository(SiteCustomer)
      .createQueryBuilder()
      .select([
            '"customerUuid" as "customerUuid"',
            // It's better to specify an alias explicitly, because the default one 
            // is implicit and may be unexpected
            'array_agg("siteUuid") as "aggregatedSiteUuids"', 
      ])
      .where('"customerUuid" IN (:customerUuids)', { customerUuids: customerUuids})
      .groupBy('"customerUuid"')

但是使用 -

baseQuery.getRawMany()

如果您沒有選擇實體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM