繁体   English   中英

TypeORM select 不区分大小写

[英]TypeORM select with case insensitive distinct

我正在尝试创建一个连接到 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')但这不起作用。 我还发现了 a.distinctOn() function 但我也不能不区分大小写。

我是 typeORM 的新手,所以我对从这里到 go 的位置有点茫然,有什么想法吗?

我正在使用 Node.JS 开发 postgresql DB,如果这有所作为的话。

这个问题的主题是 postgres 而不是 typeORM。 类似的问题: 消除 Postgres 中的重复行

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

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

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

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

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

暂无
暂无

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

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