[英]TypeORM select alias of column name
this.sampleRepo.find (
{
order: {
id: "DESC"
},
select: ['id','group']
}
);
這會按預期返回 id 和 group,但是如何將 id 作為 user_id 返回? 以及如何從組中區分 select 值?
只需在 select 字符串中添加一個別名,例如:
select: ['id AS user_id','group AS user_group']
如果上一個選項不起作用,它應該在 queryBuilder 中起作用:
this.sampleRepo
.createQueryBuilder('user')
.orderBy('user.id', 'DESC')
.select(['id AS user_id','group AS user_group'])
.getRawMany() // or .getMany()
我已經用我的一個例子(這里的最后一個)做了你需要的東西,但是寫了這個(更新。用 getRawMany 和 distinct 修復) :
getMany(): Promise<UserEntity[]> {
return this.userRepo.createQueryBuilder('user')
.where({ username: 'breckhouse0' })
.select(['DISTINCT (user.username) AS user_name', 'user.id AS user_id'])
.getRawMany();
}
這正如你所料 - 結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.