Query for most common field value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • billys1337
    New Member
    • Sep 2007
    • 1

    Query for most common field value

    Hey everyone, i'm kinda stuck at the moment with this and would appreciate any help.
    I have a table in my database logging all the search terms made by users when they my articles right. Each time a search is made it simply uses INSERT and makes a new row containing the search term, date, etc.
    However what i am trying to do now i write a script that can tell me what the most common term/terms searched for is, basically sorting through all the rows and seeing witch is the most common according to a field.
    I have never needed to do anything like this before and don't even know where to begin, can anyone help me :D, thanks allot.
    I am using PHP in case anyone cares.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    There probably is a way of using COUNT() with a sub-query of SELECT DISTINCT to achieve this,
    but I always struggle with multiple COUNTs.
    If you are using php you could SELECT the records and ORDER BY the field in question.
    Then within a loop, keep a matching field count and check for the field changing.
    When it changes you have your total for that field value, so reset the counter and continue.

    Comment

    Working...