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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:42:46+00:00 2026-05-16T03:42:46+00:00

I have two tables, with one containing records referencing the other: Goal id (int)

  • 0

I have two tables, with one containing records referencing the other:

Goal
  id (int)
  name (text)
  value_mask (text)

GoalStatus
  id (int)
  goal (int)
  created (datetime)
  value (text)

Goal.id == GoalStatus.goal

What I’d like to do, is pull the latest record from the GoalStatus table, for each record in Goal. At the moment the only way I know how to do this is by making a separate query for each record in Goal (pseudocode):

goals = db.query("SELECT * FROM Goal")

foreach (goals as goal):
    goalStatus = db.query("
        SELECT * FROM GoalStatus
        WHERE goal = " + goal.id + "
        ORDER BY created DESC
        LIMIT 1
    ")

Is there a way of condensing this, so I’m not making an extra query for each Goal?

  • 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-16T03:42:47+00:00Added an answer on May 16, 2026 at 3:42 am

    This is the per-group-maximum question. It’s a very common thing to want to do that SQL doesn’t make easy, so it gets asked a lot.

    Here’s a summary of approaches you can take. They’ll have different performance attributes, and can behave differently when there are two rows sharing the same value as the maximum.

    As a default first approach I would tend to go for a null-left-join rather than a subquery:

    SELECT ...
    FROM Goal
    JOIN GoalStatus AS Gs0 ON Gs0.goal=Goal.id
    LEFT JOIN GoalStatus AS Gs1 ON Gs1.goal=Goal.id AND Gs1.created>Gs0.created
    WHERE Goal.id=(someid)
    AND Gs1.id IS NULL
    

    that is to say, join rows where there is no other row that has a greater created value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two large tables in MySQL, one containing about 6,00,000 records and another
I have two tables one called fs_note the other called dumy_fs_note I created after
I have two tables, one is a list of tasks. The other containing historical
I have two tables in Sql Server, one containing IDs for files and the
I have two tables, one containing cities and one containing countries. Those are bi-directional
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables : one table it matchs and the other is teams.
I have two tables in MySQL, one containing a field City and one containing
I have two database tables, one for Users of a web site, containing the
I have three tables, table one (tableA) containing users data like name and email,

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.