this.sampleRepo.find(
{
order: {
id: "DESC"
},
select: ['id','group']
}
);
这将返回id
and group
符合预期,但是如何返回id
as user_id
?
以及如何从组中选择不同的值?
只需在您选择的字符串中添加别名,例如:
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 和 unique 修复):
getMany(): Promise<UserEntity[]> {
return this.userRepo.createQueryBuilder('user')
.where({ username: 'breckhouse0' })
.select(['DISTINCT (user.username) AS user_name', 'user.id AS user_id'])
.getRawMany();
}
这正如你所期望的那样工作 -results
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)