Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Results 1 to 4 of 4

This is a discussion on SQL Selecting Top Ten Most Common in the Shared & Semi-Dedicated forum
I've a feeling that I should know the answer to this, but after a long day of work my brain isn't working as fast as ...

  1. #1
    Just Walking...
    Join Date
    Oct 2002
    Location
    England
    Posts
    436

    SQL Selecting Top Ten Most Common

    I've a feeling that I should know the answer to this, but after a long day of work my brain isn't working as fast as it should.

    I've a database of surnames and want to select the 10 most common. Can this be done with one query or will I have to do some post fetch array sorting?

    Of course I know it can be done by the latter method but having mysql do it seems the preferable way.

  2. #2
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    SELECT Surname, COUNT(*) FROM TABLENAME GROUP BY Surname;

    should do the trick...

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  3. #3
    Just Walking...
    Join Date
    Oct 2002
    Location
    England
    Posts
    436
    Aye that will still need to be sorted after fetching though to give them in the right order. However I've resigned myself to that and have just sorted the resultant array using usort.

  4. #4
    JPC Member
    Join Date
    May 2003
    Location
    Louisville, KY
    Posts
    5
    SELECT TOP 10 Surname, Count(Surname)
    From Tablename
    Group By Surname
    Order By Count(Surname) Desc;

    That should work...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •