【问题标题】:TypeORM Custom Repository, select distinct values from specified columnTypeORM 自定义存储库,从指定列中选择不同的值
【发布时间】:2020-08-18 18:20:31
【问题描述】:

在我使用 nestjs + typeorm + postgresql 的后端中,我有一个 CustomRepository 并且想要替换一些普通的 sql 查询。

这就是我所拥有的

const sqlQuery = `SELECT DISTINCT "myColumn" FROM "myTable"`
const sqlRes = await this.query(sqlQuery);

我正在尝试得到这样的东西

this.find({select:["myColumn"]}); // note the missing DISTINCT

但这给了我完整的列,但我只想要 DISTINCT 值。

我发现了很多奇怪的 createQueryBuilder().select(DISTINCT "myColumn" FROM "...... etc... 解决方案,这些解决方案并没有真正让我比我的工作解决方案有任何好处。

【问题讨论】:

    标签: javascript postgresql nestjs typeorm


    【解决方案1】:

    你可以这样做:

    await getManager().createQueryBuilder('entity')
      .select('column')
      .distinct(true)
      .getRawMany();
    

    【讨论】:

      猜你喜欢
      • 2020-07-02
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      • 2015-12-04
      • 2021-01-07
      • 2021-06-07
      • 2022-07-07
      • 2022-11-09
      相关资源
      最近更新 更多