php Averages

Discussion in 'PHP' started by Silver89, Mar 13, 2009.

  1. #1
    Hey,

    I need to find the:

    "Mode - The most common value in a set of data."

    From a set of results from a mysql table OR an array, does anyone know of any existing functions to help do this?

    Many Thanks
    -Dan
     
    Silver89, Mar 13, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    SELECT value, COUNT(value) FROM table GROUP BY value ORDER BY COUNT(value) DESC LIMIT 1;
     
    SmallPotatoes, Mar 13, 2009 IP
    Silver89 likes this.
  3. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #3
    Actually it works great, thanks! :)
     
    Silver89, Mar 13, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That assumes that you are looking at a single table and the field named "value" contains the quantity for which you want to take the mode. If your data structure is more complicated and you are having a hard time adapting the query then it would be helpful to describe your data.
     
    SmallPotatoes, Mar 13, 2009 IP