![](/img/trans.png)
[英]TypeORM find where clause, how to add where in multiple parameter
[英]Typeorm How Can I get Min field with Where clause?
我在我的項目中使用 Typeorm 和 nestjs。 我有架構:
export class GroupEntity implements IGroup {
// Attributes
@PrimaryGeneratedColumn({ type: 'string' })
public id: string;
@Column({ type: 'string', nullable: false })
public userId: string;
@Column({ type: 'int2' })
public syncStatus: number;
}
我想通過 userId 獲取最小同步狀態,其中 syncStatus 不為空
示例數據:
[{
id: "1",
userId: "101",
syncStatus: 2
}, {
id: "2",
userId: "101",
syncStatus: 3
}, {
id: "2",
userId: "105",
syncStatus: -5
}, {
id: "2",
userId: "101",
syncStatus: null
}]
預期結果
2
or row with value 2
非常感謝!
您只需要使用MIN
,方法如下:
return this.groupRepository.createQueryBuilder('group')
.select("MIN(group.syncStatus)",'syncStatus')
.addSelect(['group.id','group.userId'])
.where('group.userId = :user", {
user,
})
.andWhere('group.syncStatus is not null')
.getRawOne();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.