列出重复值的数量 [英] List number of duplicate values

查看:85
本文介绍了列出重复值的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Rails ActiveRecord关联,其中一课有很多书籍,而书籍属于一课。我想运行一个SQL命令,该命令将告诉我有多少课属于该课程。

I have the following a Rails ActiveRecord association where a lesson has_many books, and books belong_to a lesson. I want to run a SQL command that will tell me how many lessons have multiple books that belong to that lesson.

例如:第1课有第1本书和第2本书,我想看看发生了多少次。

Ex: Lesson 1 has books 1 and 2, and I want to see how many times that happens.

推荐答案

#having 可让您执行#group 过滤掉只有一本书的课程。

#having will let you do a #group that filters out Lessons that only have one Book.

Book.group(:lesson_id).having("count(lesson_id) > 1").count

这篇关于列出重复值的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆