首页 文章

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

提问于
浏览
0

在我的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 + '%' })

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

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

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

相关问题