On Fri, Apr 06, 2001 at 08:26:28PM -0400, Sean Cooper wrote:
> Hello all --
> 
> I'm sorry if this is off topic, but does anybody know of a way to find
> the most common entry in a database using an SQL query? I'm using MySQL
> to cache search engine terms and want to find the 5 most common terms
> ....
> 
> I can do it in perl pretty easily using a hash to store the counts of
> each value, but don't want to do it that way unless there is no way to do
> it in SQL.

Here's how I would accomplish this:

SELECT term, count(*)
FROM   search_terms
GROUP BY term
ORDER BY count(*)

And the first five rows returned would contain the five most common terms.


Ronald

Reply via email to