有没有一种方法可以将Org By Orm Repository用于“分组依据”和“计数”

时间:2020-07-29 16:11:36

标签: graphql nestjs typeorm

我是这里的新人,最近加入了ORM类型的新人,我正在尝试的代码

FIRST QUERY: I would like to use this approach but not sure how I can group by with count on the column and then order by desc on that column

 const result = await this.jobViewsRepository.find({
        relations: ["jobs"],
        loadEagerRelations: true,  
        order: { id: "DESC" },
        skip: offset,
        take: limit,
    }
    );

我正在尝试是否可以在上面的查询中使用它

 **SECOND QUERY:** **ITS WORKING FOR ME PERFECTLY THE RESULT I AM LOOKING** 

    const res = await this.jobViewsRepository.createQueryBuilder('jobViews')           
    .addSelect("COUNT(jobViews.user_id) AS jobViews_total_count" )
    .leftJoinAndSelect(Jobs, "jobs", "jobs.id = jobViews.job_id")
    .where("jobs.user_id != :id", { id: user_id })        
    .groupBy("jobViews.job_id")**
    .orderBy('jobViews_total_count', 'DESC')**
    .limit(limit)
    .offset(offset)           
    .getRawMany();

如果有什么可以帮助我的,将不胜感激

谢谢

2 个答案:

答案 0 :(得分:0)

至少在当前版本中没有办法做到这一点(无论是在文档中还是在网络中)

答案 1 :(得分:0)

您使用 findAndCount 计算结果大小

   result = await this.jobViewsRepository.findAndCount({ ... })

结果 = [数据,计数]