views:

105

answers:

4

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
A: 
class Model < ActiveRecord::Base

  named_scope :recent, :conditions => ["created_at > ?", DateTime.now - 2.hours], :limit => 5, :order => "desc"

end

Model.recent
khelll
+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
Does that realy work??
Lichtamberg
It depends on what he means by "most occurring 5 records"....
khelll
One would assume the 5 which occur the most.
Trejkaz
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