查询和参数
2026/1/17小于 1 分钟
查询和参数
基础查询 where
参数放在where 里面
await this.userRepository.findOne({where: [{phone}]})添加查询关系 连表查询 relations
const user = await this.userRepository.findOne({
where: {phone},
relations: ['role'],
})分页查询 skip take
const data = await this.giftRepository.find({
where: {name: ILike(`%${param.name}%`)},
order: {id: 'ASC'},
take: param.pageSize,
skip: (param.pageNum - 1) * param.pageSize,
})模糊查询 ILike
const data = await this.giftRepository.find({
where: {name: ILike(`%${param.name}%`)},
})排序 order
const data = await this.giftRepository.find({
where: {name: ILike(`%${param.name}%`)},
order: {id: 'ASC'},
})createQueryBuilder 自定义更高级的查询
const data = await this.lotteryChancesRepository
.createQueryBuilder('lotteryChances')
.where('lotteryChances.user_id = :id', {id})
.getOne()