suppose there are 3 fields in a mysql table. id, word and date. the word field is not a unique field, so many records have the same word. so how do i find out which word is repeated in records the most? exactly. the most repeated 5 words.. thanks
+1
A:
Model.find(:all, :conditions => ["created_at > ?", DateTime.now - 2.hours], :limit => 5, :order => "desc")
Lichtamberg
2009-10-02 09:17:47
A:
class Model < ActiveRecord::Base
named_scope :recent, :conditions => ["created_at > ?", DateTime.now - 2.hours], :limit => 5, :order => "desc"
end
Model.recent
khelll
2009-10-02 10:48:43
+1
A:
Both the answers above seem to print just the most recent record. I think to get the most frequent you will need to use more SQL. :-(
Assuming you have a model called Message with a field called 'text' which contains the messages...
Message.find(:all, :select => '*, COUNT(*) AS count',
:group => 'text',
:order => 'count DESC',
:limit => 5 )
Disclaimer: I have never tried doing this, except to verify that the code itself runs.
Trejkaz
2009-10-02 11:07:17
Does that realy work??
Lichtamberg
2009-10-02 13:26:32
It depends on what he means by "most occurring 5 records"....
khelll
2009-10-02 15:43:51
One would assume the 5 which occur the most.
Trejkaz
2009-10-03 08:49:03
A:
No no! thats not the way.. all i am asking is.. In one field the data is not unique.. and how to find the most number of records that has the same value.
Maxsy
2009-10-02 17:57:54