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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:06:53+00:00 2026-05-31T08:06:53+00:00

I have 2 tables, one containing information on a city (in a game) and

  • 0

I have 2 tables, one containing information on a city (in a game) and another containing scores as recorded throughout periods of the day. Table examples have been simplified.

CREATE TABLE cities(
  cid INT(100),
  name VARCHAR(100),
  updatedAt DATETIME
);
CREATE TABLE cl_scores(
  cid INT(100),
  score INT(255),
  updatedAt DATETIME
);

I’m trying to gather a list of scores for a particular city. The list should only contain the MAX(updatedAt) date and score for every day. Here is what I have so far:

SELECT a.cid, b.name, a.score, a.updatedAt FROM ci_scores AS a 
JOIN cities AS b ON a.cid = b.cid 
JOIN (SELECT MAX(updatedAt) AS maxUpdatedAt FROM ci_scores 
GROUP BY DATE(updatedAt)) AS L ON DATE(L.maxUpdatedAt) = DATE(a.updatedAt) 
WHERE a.cid = 10158241 ORDER BY a.updatedAt ASC;

However, it isn’t returning a single entry for each day. It is in fact returning every row in the ci_scores table and simply adding the name field as shown below:

+----------+---------+-------+---------------------+
| cid      | name    | score | updatedAt           |
+----------+---------+-------+---------------------+
| 10158241 | Genesis |  3087 | 2012-03-13 04:04:03 |
| 10158241 | Genesis |  3207 | 2012-03-13 17:48:56 |
| 10158241 | Genesis |  3255 | 2012-03-14 00:44:11 |
| 10158241 | Genesis |  3262 | 2012-03-14 10:21:05 |
| 10158241 | Genesis |  3262 | 2012-03-14 13:42:42 |
+----------+---------+-------+---------------------+

What am I doing wrong in my query???

Solutions Tried

SELECT a.cid, b.name, a.score, a.updatedAt FROM ci_scores AS a 
JOIN cities AS b ON a.cid = b.cid 
JOIN (SELECT MAX(updatedAt) AS maxUpdatedAt FROM ci_scores 
GROUP BY DATE(updatedAt) LIMIT 1) AS L ON L.maxUpdatedAt = a.updatedAt
WHERE a.cid = 10158241 ORDER BY a.updatedAt ASC;

This did not work, removing the DATE() = DATE() makes it return 0 records as the times are different. If I add that back in it returns:

+----------+---------+-------+---------------------+
| cid      | name    | score | updatedAt           |
+----------+---------+-------+---------------------+
| 10158241 | Genesis |  3087 | 2012-03-13 04:04:03 |
| 10158241 | Genesis |  3207 | 2012-03-13 17:48:56 |
+----------+---------+-------+---------------------+

This is not correct, I need 1 record for each day and that record being the latest entered on that day.

  • 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-31T08:06:54+00:00Added an answer on May 31, 2026 at 8:06 am

    I think you should join your subset by updateAT without DATE function

    SELECT a.cid, b.name, a.score, a.updatedAt FROM ci_scores AS a 
    JOIN cities AS b ON a.cid = b.cid 
    JOIN (SELECT MAX(updatedAt) AS maxUpdatedAt FROM ci_scores 
    GROUP BY DATE(updatedAt)) AS L ON L.maxUpdatedAt = a.updatedAt
    WHERE a.cid = 10158241 ORDER BY a.updatedAt ASC;
    

    UPDATE:
    There is another error that in the sub query you need to group by the city id as well so that you can get the correct max date:

    SELECT a.cid, b.name, a.score, a.updatedAt FROM ci_scores AS a 
    JOIN cities AS b ON a.cid = b.cid 
    JOIN (SELECT MAX(updatedAt), cid AS maxUpdatedAt FROM ci_scores 
    GROUP BY DATE(updatedAt), cid) AS L ON L.maxUpdatedAt = a.updatedAt AND L.cid = a.cid
    WHERE a.cid = 10158241 ORDER BY a.updatedAt ASC;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing persons information (one row per person) and another table
I have two large tables in MySQL, one containing about 6,00,000 records and another
I have four different tables, one main SPECIAL table containing only id's that reference
I have one table containing user sessions and another to indicate violations in the
I have a (DB2) database table containing location information, one column of which is
hope this makes sense. I have merged together two tables one containing information about
I have two tables, one table has some information in each row along with
I have two tables, one containing cities and one containing countries. Those are bi-directional
I have two tables in Sql Server, one containing IDs for files and the
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work

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.