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

  • Home
  • SEARCH
  • 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 8941923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:24:40+00:00 2026-06-15T11:24:40+00:00

I have 2 MYSQL tables, users and scores . Detail: users table: scores table:

  • 0

I have 2 MYSQL tables, users and scores. Detail:

  • users table:

enter image description here

  • scores table:

enter image description here

My intention is get 20 users list that have point field sort DESC (descending) combine avg_time field sort ASC (ascending). I use the query:

SELECT users.username, scores.point, scores.avg_time
FROM scores, users
WHERE scores.user_id = users.id
GROUP BY users.username
ORDER BY scores.point DESC, scores.avg_time
LIMIT 0, 20

The result is:

enter image description here

The result is wrong because the first line is exactly point = 100 and avg_time = 60.

My desired result is:

username    point    avg_time
demo123      100        60
demo123456   100       100
demo         90        120

I tried many times with different queries but the result is still wrong. Could you give me some solutions?

  • 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-15T11:24:41+00:00Added an answer on June 15, 2026 at 11:24 am

    Ok, I THINK I understand what you want now, and let me clarify to confirm before the query. You want 1 record for each user. For each user, you want their BEST POINTS score record. Of the best points per user, you want the one with the best average time. Once you have all users “best” values, you want the final results sorted with best points first… Almost like ranking of a competition.

    So now the query. If the above statement is accurate, you need to start with getting the best point/average time per person and assigning a “Rank” to that entry. This is easily done using MySQL @ variables. Then, just include a HAVING clause to only keep those records ranked 1 for each person. Finally apply the order by of best points and shortest average time.

    select
          U.UserName,
          PreSortedPerUser.Point,
          PreSortedPerUser.Avg_Time,
          @UserRank := if( @lastUserID = PreSortedPerUser.User_ID, @UserRank +1, 1 ) FinalRank,
          @lastUserID := PreSortedPerUser.User_ID
       from
          ( select
                  S.user_id,
                  S.point,
                  S.avg_time
               from
                  Scores S
               order by
                  S.user_id,
                  S.point DESC,
                  S.Avg_Time ) PreSortedPerUser
             JOIN Users U
                on PreSortedPerUser.user_ID = U.ID,
          ( select @lastUserID := 0,
                   @UserRank := 0 ) sqlvars 
       having
          FinalRank = 1
       order by
          Point Desc,
          Avg_Time
    

    Results as handled by SQLFiddle

    Note, due to the inline @variables needed to get the answer, there are the two extra columns at the end of each row. These are just “left-over” and can be ignored in any actual output presentation you are trying to do… OR, you can wrap the entire thing above one more level to just get the few columns you want like

    select 
          PQ.UserName,
          PQ.Point,
          PQ.Avg_Time
       from
          ( entire query above pasted here ) as PQ
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using php and mysql I have two tables, a users table and a profiles
Using MySQL, I have a simple table that logs the IP Address that users
I have a MySql table that stores the users state and city from a
I have a table that stores scores from users of my game - what
I have two mysql tables: /* Table users */ CREATE TABLE IF NOT EXISTS
I have three different tables in my MySQL database. table users: (id, score, name)
I have a MYSQL-Table which stores scores. Every time a user improves a new
I have a MySQL table called Users. The rows have 4 fields and look
Lets say I have a MySQL table, Users, like this - ----------------------------------------- ID |
So I have a mySQL database with a 'users' table which includes a username,

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.