【问题标题】:TypeORM select with case insensitive distinctTypeORM select with case insensitive distinct
【发布时间】:2020-11-03 17:24:27
【问题描述】:

我正在尝试创建一个连接到 postgresql 数据库的 TypeORM 查询构建器,以获取数据库中的所有唯一名称。我的查询看起来像这样

names = await this._context.manager
        .getRepository(Names)
        .createQueryBuilder('names')
        .select('DISTINCT ON (names.name) names.name')
        .orderBy('names.name', 'ASC')
        .getRawMany();

目前,此查询会获取数据库中的所有名称,但它区分大小写,因此它无法从“jane doe”中挑选出像“Jane Doe”这样的重复项。到目前为止,我已经尝试像这样制作不同的大写/小写: .select('DISTINCT ON LOWER(names.name) names.name') 但这不起作用。我还找到了一个 .distinctOn() 函数,但我也不能不区分大小写。

我是 typeORM 的新手,所以我不知道从哪里开始,有什么想法吗?

我正在使用 Node.JS 开发一个 postgresql 数据库,如果这有影响的话。

【问题讨论】:

    标签: javascript node.js postgresql typeorm


    【解决方案1】:

    这个问题的主题是 postgres 而不是 typeORM。 类似问题:Eliminating Duplicate rows in Postgres.

    在您的情况下,正确的语法是:

    names = await this._context.manager
            .getRepository(Names)
            .createQueryBuilder('names')
            .select('DISTINCT ON (LOWER(names.name)) names.name')
            .orderBy('LOWER(names.name)', 'ASC')
            .getRawMany();
     
    

    【讨论】:

    • 是的,这就像一个魅力 :) 我之前尝试过 LOWER,但我也错过了将它添加到 orderBy 中,所以我没有得到任何结果。非常感谢!
    【解决方案2】:

    使用 Repository,我以这种方式使用 ILIKE 运算符将 postgres 作为数据库进行了不区分大小写的搜索

    const planning_groups: any[] =
        await this.planningGroupRepository.find({
            where: `"tenant_id" ILIKE '${tenantId}'` 
        });
    

    其中 tenant_id 是列名,tenantId 是要搜索的关键字。

    【讨论】:

      猜你喜欢
      • 2010-10-07
      • 2017-11-25
      • 1970-01-01
      • 2012-05-10
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多