在TypeORM QueryBuilder中使用通配符进行LIKE查询

问题描述 投票:0回答:1

在我的NestJS项目中,我有这个TypeORM查询:

const users = await this.usersRepository.find({
  skip,
  take,
  order: sortingObject,
  join: {
      alias: 'user',
      leftJoinAndSelect: {
          country: 'user.country_id',
      },
  },
});

现在我只想用名字中的John返回用户。在SQL中,这将是一个LIKE查询LIKE %John%

https://github.com/typeorm/typeorm/blob/master/docs/find-options.md,没有关于通配符LIKE查询的信息。

How to perform a like query Typeorm给出了一个解决方案: .where("user.firstName like :name", {name: '%' + firstName + '%' })

但是当我使用skip而不是take时,我无法使用where()find()

有关如何使用TypeORM QueryBuilder实现此目的的任何想法?

nestjs typeorm
1个回答
2
投票

QueryBuilder中有分页方法(.skip(int).take(int))。

尝试这样的事情。

const users = await this.usersRepository
    .createQueryBuilder("user")
    .leftJoinAndSelect("user.country_id", "country")
    .skip(5)
    .take(10)
    .where("user.firstName like :name", {name: '%' + firstName + '%' })
    .orderBy("user.id", "DESC")
    .getMany();

有关详细信息,请参阅文档:Using Pagination in TypeORM QueryBuilder

© www.soinside.com 2019 - 2024. All rights reserved.