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 582047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:38:42+00:00 2026-05-13T14:38:42+00:00

I have two tables User id name Result id user_id score created_at Every hour

  • 0

I have two tables

User
  id
  name

Result
  id
  user_id
  score
  created_at

Every hour a cron job runs and puts data in the result table for each user – assigning them a score based on some external variable parameters.

So the Result table contains many records for each user.

I want to retreive a ‘top ten’ users at any given point. So only one row for each user should be returned, and it should contain their most recent available score, and ideally only 10 rows relating to those ten users with the highest scores.

I currently fetch all results and do the leg work with php which I’m fairly sure would be faster and more efficient if handled by the database. But I don’t know where to start.

  • 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-05-13T14:38:43+00:00Added an answer on May 13, 2026 at 2:38 pm
    SELECT u.id,
           u.name,
           r.score
    FROM Result r
    JOIN
      (SELECT user_id, MAX(created_at) AS max_date
       FROM Result
       GROUP BY user_id) lr ON r.user_id=lr.user_id
    JOIN User u ON u.id=r.user_id
    WHERE r.created_at=lr.max_date
    ORDER BY r.score DESC LIMIT 10;
    

    A sample run:

    mysql> insert into User (name) values ('foo'), ('bar');
    mysql> insert into Result (user_id, score, created_at) values (1,100,'2010-01-20'), (2,150,'2010-01-20'),(1,150,'2010-01-21'),(2,100,'2010-01-21');
    mysql> SELECT u.id,        u.name,        r.score FROM Result r JOIN   (SELECT user_id, MAX(created_at) AS max_date    FROM Result    GROUP BY user_id) lr ON r.user_id=lr.user_id JOIN User u ON u.id=r.user_id WHERE r.created_at=lr.max_date ORDER BY r.score DESC LIMIT 10;
    +----+------+-------+
    | id | name | score |
    +----+------+-------+
    |  1 | foo  |   150 | 
    |  2 | bar  |   100 | 
    +----+------+-------+
    2 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables defined as the following: User (ID int, name varchar) Hours
I have two tables. Table user contains user_id, user_name. Table Transactions contains user_id, transactions
I have two tables: Users: ID, first_name, last_name Networks: user_id, friend_id, status I want
I have Two tables. 1.Users table (Username , Name) 2.Picture table( ID , Username
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
I've got the following problem: I have two tables: (simplified) +--------+ +-----------+ | User
I have a simple database with two tables. Users and Configurations. A user has
We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType
I have two tables: Users and Roles. A user may have more roles, so
I have a SQL database with two tables like this: Users Id (PK) Name

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.