簡體   English   中英

Typeorm 如何使用 Where 子句獲取 Min 字段?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM