Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8304783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:56:07+00:00 2026-06-08T17:56:07+00:00

I’ve got a possibly ‘classic’ mysql max/group by question Here is my basic table

  • 0

I’ve got a possibly ‘classic’ mysql max/group by question
Here is my basic table structure.

id | userid | username | date | score
1 | 1111 | joe | 2012-05-16 | 1000
2 | 2222 | john | 2012-03-17 | 2000
3 | 3333 | jill | 2012-02-11 | 3000
4 | 2222 | john | 2012-12-25 | 5000
5 | 3333 | jill | 2012-04-08 | 4000
6 | 1111 | joe | 2012-06-17 | 1000
7 | 1111 | joe | 2012-01-14 | 500
8 | 2222 | john | 2012-06-29 | 6000
9 | 4444 | bill | 2012-08-08 | 4000

I would like to get each user’s highest score.
Each user should have only one score displayed in the result.

My desired result is
id | userid | username | date | score
8 | 2222 | john | 2012-06-29 | 6000
9 | 4444 | bill | 2012-08-08 | 4000
5 | 3333 | jill | 2012-04-08 | 4000
6 | 1111 | joe | 2012-06-17 | 1000

If the user’s highest score appears more than once (ex id 1&6), the most recent date should be displayed.

This is my current query

select s.*
from scores as s
inner join (
    select userid, max(score) as max_score
    from scores
    group by userid
    ) t on s.userid = t.userid and s.score = t.max_score
order by score DESC, date DESC

Which returns the incorrect result of

id | userid | username | date | score
8 | 2222 | john | 2012-06-29 | 6000
9 | 4444 | bill | 2012-08-08 | 4000
5 | 3333 | jill | 2012-04-08 | 4000
6 | 1111 | joe | 2012-06-17 | 1000
1 | 1111 | joe | 2012-05-16 | 1000

Any suggestions on how to resolve this?

I should also mention that the relationship between userid and username is not one to one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:56:08+00:00Added an answer on June 8, 2026 at 5:56 pm

    This should give you the result you are looking for. Since MySql doesn’t have a PARTITION function as SQL SERVER, I used a known work around.

    SELECT tmp.id, tmp.userid,tmp.username,tmp.`date`,score FROM
       (SELECT *, IF(@prev<>userid, @rownum := 1, @rownum := @rownum+1 ) AS rank,
               @prev := userid
        FROM scores s0
        JOIN (SELECT @rownum := NULL, @prev := 0) AS r
        ORDER BY s0.userid, s0.score DESC, s0.`date` DESC
       ) AS tmp
    WHERE tmp.rank=1
    ORDER BY tmp.score DESC, tmp.`date` desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables with like below codes: Table: Accounts id | username |
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
i got an object with contents of html markup in it, for example: string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to select an H1 element which is the second-child in its group
I don't have much knowledge about the IPv6 protocol, so sorry if the question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.