我有一个有技能的用户
@Entity("users")
export class UserEntity {
@Column('text', { array: true, nullable: true, })
skills: string[]
}
我需要寻找具有一定技能的用户
但我需要搜索部分匹配,例如使用 ILike("%${query}%") 但仅适用于数组时
您可以使用以下查询在字符串数组中进行部分搜索:
SELECT *
FROM "users"
WHERE EXISTS (
SELECT 1
FROM unnest("skills") s
WHERE s LIKE '%${searchString}%'
);
unnest
函数将数组分解为我们执行条件的各个字符串元素。
上述 sql 语句的
typeorm
查询将是:
const users = await this.userRepo.createQueryBuilder('user')
.where('EXISTS(SELECT 1 FROM unnest("user"."skills") s WHERE s LIKE :searchString)', {
searchString: `%${searchString}%`,
})
.getMany();