TypeORM select alias of column name

后端 未结 1 1778
感动是毒
感动是毒 2021-01-25 07:35
this.sampleRepo.find (
            {
                
                order: {
                    id: "DESC"
                },
                select: [\'id\         


        
相关标签:
1条回答
  • 2021-01-25 08:12

    Just add an alias in you select string, e.g.:

    select: ['id AS user_id','group AS user_group']
    

    If the previous option didn't work, it should work in queryBuilder:

    this.sampleRepo
          .createQueryBuilder('user')
          .orderBy('user.id', 'DESC')
          .select(['id AS user_id','group AS user_group'])
          .getRawMany() // or .getMany()
    
    

    I've made smth as you need with one of my examples (last one here) but wrote this (upd. fixed with getRawMany and 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();
      }
    

    and this works as you expect - results

    0 讨论(0)
提交回复
热议问题