【问题标题】:Is there a way I can use Group By and Count with Type Orm Repository有没有办法可以将 Group By 和 Count 与 Type Orm Repository 一起使用
【发布时间】:2020-11-19 07:08:18
【问题描述】:

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

FIRST QUERY:我想使用这种方法,但不确定如何按列上的 count 分组,然后按该列上的 desc 排序

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

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

第二个问题:我正在寻找的结果完全适合我

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

如果有人可以帮助我,我们将不胜感激

谢谢

【问题讨论】:

    标签: nestjs typeorm


    【解决方案1】:

    我相信你可以使用 .query 来编写你自己的查询

    【讨论】:

      【解决方案2】:

      您使用findAndCount 计算结果大小

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

      结果 = [数据,计数]

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-01-15
          • 1970-01-01
          • 2011-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-05
          相关资源
          最近更新 更多