Is there a way I can use Group By and Count with Type Orm Repository

邮差的信 提交于 2020-12-09 06:42:11

问题


I am new here recently joined and New in Type ORM My code that I am trying

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,
    }
    );

I am trying if I can use this in my above query

 **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();

Please if any can help me out in this will be really appreciated

Thanks

来源:https://stackoverflow.com/questions/63157900/is-there-a-way-i-can-use-group-by-and-count-with-type-orm-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!