[MySQL] Select all rows & data with the highest number for one name

Discussion in 'MySQL' started by Kellerkind, Dec 6, 2007.

  1. #1
    I store the history, including all changes of my members profiles in a database table. For each member the "Changes" column increments by 1 each time they update their profile. So the latest and most recent information about a member is in the row with the highest "Changes" count.

    In my example below this would be always the row highlighted with a *

    Changes - Name - Balance

    4 - Burt - 14$
    *5 - Burt - 19$

    1 - Frank - 15$
    2 - Frank - 11$
    *3 - Frank - 12$

    1 - Steve - 50$
    *2 - Steve - 20$

    How would a query look if I would want to list all members and their latest information in a list? The list should be ordered by their "Balance" like this:

    Steve - 20
    Burt - 19
    Frank - 12

    Any help would be awesome because this drives me nuts since hours. I tried all sorts of stuff with group by and max but can't work it out.
     
    Kellerkind, Dec 6, 2007 IP
  2. Kellerkind

    Kellerkind Active Member

    Messages:
    160
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    The solution is:

          SELECT * FROM (
              SELECT * FROM table
              ORDER BY Changes DESC
           ) temp
          GROUP BY Name
    Code (markup):
    for all those who have a similar problem
     
    Kellerkind, Dec 6, 2007 IP