订购和限制 [英] order by and limit
本文介绍了订购和限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要做以下查询:
SELECT * FROM "specimens" ORDER BY distribution_sheet_id DESC LIMIT 10
我已经把:
<%= Specimen.last(:order => :distribution_sheet_id).id %>
我喜欢输出'limit 10'而不是limit 1.我想它是.last,但我可以用其他方式表达它来限制10.
I like to output 'limit 10' not limit 1. I suppose its the .last, but can I express it in other way to limit 10.
谢谢
推荐答案
假设您使用的是 Rails 3
Assuming you are using Rails 3
<%= Specimen.limit(10).order("distribution_sheet_id").all %>
请注意,如果限制超过1条记录,最后不能调用#id
,因为结果是一个数组.
Please note that if you limit to more than 1 record, you can't call #id
at the end because the result is an array.
获取所有ID
<%= Specimen.limit(10).order("distribution_sheet_id").map(&:id) %>
<小时>
对于 Rails 2.3,使用旧的基于哈希的条件.
For Rails 2.3 use the old hash-based conditions.
<%= Specimen.all(:order => "distribution_sheet_id", :limit => 10) %>
同样适用于 id
<%= Specimen.all(:order => "distribution_sheet_id", :limit => 10).map(&:id) %>
这里我使用 #to_sentence
方法来连接所有 ID.根据您的用例调整代码.
Here I'm using the #to_sentence
method to join all ID. Adapt the code according to your use case.
<%= Specimen.all(:order => "distribution_sheet_id", :limit => 10).map(&:id).to_sentence %>
这篇关于订购和限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文