group by - How must the mysql query be like to achieve the shown result? -


Say, I have two tables like this:

  table group table user + - - + ----------- + + ---- + ---------- + ------ + ---------- + | ID | GroupName | | ID | Username Rank | Group_ID | + ---- + ----------- + + ---- + ---------- + ------ + -------- - + | 1 | Friends | | 1 | Frank | 1 | 1 | | 2 | Family | 2 | Mike | 3 | 1 | + ---- + ----------- + | 3 | Steve | 2 | 1 | | 4 | Tom | 1 | 2 | + ---- + ---------- + ------ + ---------- +  

And I want to Select all the groups and get the user with the highest rank (highest number) for each group, so basically I have to get this result:

  + ------ ----------- + ---------- + - -------- + --------------- + | Group.groupname | Group.id | | User.id | User.username | + ----------------- + ---------- + --------- + ---------- ----- + | Friends | 1 | 2 | Mike | | Family 2 | 4 | Tom | + ----------------- + ---------- + --------- + ---------- ----- +  

How to choose? It's probably very easy, but I'm not getting it right now ....

  G-group name, u .group_id, user_id as u.id, UU.U. As a Group G Inner Join (Group_ID, Max (Rank) by Group_ id, select MaxRec as User Group) GUID = um.group_id Innum Join User U on um.group_id = u.group_id and um.MaxRank = u Rank  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -