Re: largest number of entries
| From: | Steve Edberg | Date: | Mon, 09 Oct 2000 00:48:43 +0000 |
| Subject: | Re: largest number of entries | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-19129@lists.php.net to get a copy of this message | ||
At 3:40 PM -0700 10/8/00, Tyler Longren wrote:
Hey everybody, Let's say that I have a mysql table, inside this table, there is a field named 'name'. And let's say there are 10 different names in that field, each in a different row. If the name 'Tyler' is in there 5 times, the name 'Ronald' is in there 9 times, the name 'Vic' is in there 12 times, the name 'Kristin' is in there 6 times, and the name 'Paul' is in there 8 times. That's 5 names, the other 5 names in there are only listed 1 time. How could I get the 5 highest entries? which would be: Vic - 12 Ronald - 9 Paul - 8 Kristin - 6 Tyler - 5 ??? That was pretty darn unclear, so if you don't understand what I mean, just forget about it. :)select name, count(name) as n from your_table_name_here group by count(name) order by n desc limit 5 should work (untested, though). Note that (at least in older MySQL versions) you _must_ user 'count(name)' in the group clause, instead of the alias 'n'. -steve +--- "They've got a cherry pie there, that'll kill ya" ------------------+
| Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ |+-------------------------------------- FBI Special Agent Dale Cooper ---+