ActiveRecord的发现 - 跳过记录或获取每N记录 [英] ActiveRecord Find - Skipping Records or Getting Every Nth Record

查看:109
本文介绍了ActiveRecord的发现 - 跳过记录或获取每N记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的,我选择一组数据的查询,但我希望能够再降低该数据的分辨率只有选择,也就是说,每三个记录,或者甚至每百个记录,或什么的。

I'd like to do a query where I select a bunch of data, but I'd like to be able to then decrease the resolution of that data by only selecting, say, every third record, or maybe even every hundredth record, or whatever.

有没有简单的方法来用ActiveRecord做到这一点?

Is there any straightforward way to do this with ActiveRecord?

推荐答案

在甲骨文,我会写如下:

In Oracle i would write that as follows:

YourModel.find(:conditions => 'MOD(ROWNUM,3) = 0') 

这有过滤器发生在数据库的优势,所以不是一切都恢复。

this has the advantage that the filter happens at the database, so not everything is retrieved.

在PostgreSQL里这叫做 ROW_NUMBER (实际上,它是SQL标准)。在MySQL中,这是不支持的。

In PostgreSQL this is called ROW_NUMBER (actually that is the SQL-standard). In MySQL this is not supported.

在MySQL中,你可以使用 SELECT @rownum模仿ROWNUM:= @ ROWNUM + 1 ROWNUM,T * FROM(SELECT @rownum:= 0),R,mytable的吨; 。所以我想是这样

In mysql you can mimic rownum using SELECT @rownum:=@rownum+1 rownum, t.* FROM (SELECT @rownum:=0) r, mytable t;. So i guess something like

Bar.find_by_sql("select * from (SELECT @rownum:=@rownum+1 rownum, t.* FROM (SELECT @rownum:=0) r, mytable t) where mod(rownum,3) = 0") 

应该工作。

这篇关于ActiveRecord的发现 - 跳过记录或获取每N记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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