如何计算 ROR 中特定字段中具有唯一值的记录数? [英] How can I count the number of records that have a unique value in a particular field in ROR?

查看:13
本文介绍了如何计算 ROR 中特定字段中具有唯一值的记录数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含日期字段的记录集,我想确定记录集中代表了多少个唯一日期.

I have a record set that includes a date field, and want to determine how many unique dates are represented in the record set.

类似于:

Record.find(:all).date.unique.count 

当然,这似乎行不通.

推荐答案

你想要的是以下 SQL:

What you're going for is the following SQL:

SELECT COUNT(DISTINCT date) FROM records

ActiveRecord 内置了这个:

ActiveRecord has this built in:

Record.count('date', :distinct => true)

这篇关于如何计算 ROR 中特定字段中具有唯一值的记录数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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