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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:01:38+00:00 2026-06-14T11:01:38+00:00

Here is a simplified look at the problem I am trying to cleanly solve

  • 0

Here is a simplified look at the problem I am trying to cleanly solve via a MySQL query. This is not the actual table I am dealing with.

If I have the following table:

Name Buyer ID  
John Fred  4  
John Smith 3  
Fred Sally 2  
John Kelly 1

I would like a query to return the following:

Name Buyer ID      
John Fred  4  
Fred Sally 2  

Such that we group by ‘name’ and show the latest row / buyer / ID.

I tried to implement this by performing a nested select statement, wherein I first performed “ORDER BY ID DESC” then, on the outermost SELECT, “GROUP BY NAME”. And, while this is a roundabout way of solving the problem, it seemed that, by virtue of the ordering, the correct selection would be returned to me. Unfortunately, “GROUP BY” does not ‘guarantee’ that the ‘Buyer’ column will contain the expected entry.

Any helpful suggests for implementing this as a query? At the moment, I have a highly-inefficient PHP ‘version’ of the query running on a large table dump – definitely not the best choice.

  • 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-14T11:01:39+00:00Added an answer on June 14, 2026 at 11:01 am

    Try this one, the idea behind the subquery is that it gets the latest ID for each Name using MAX (aggregate function). Then join it against the table itself on the two columns of the subquery.

    SELECT  a.*
    FROM    tableName a
            INNER JOIN 
            (
                SELECT name, MAX(ID) maxID
                FROM tableName
                GROUP BY name
            ) b ON a.Name = b.Name AND
                    a.ID = b.MaxID
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a simplified example of my problem. I have a table where there's a
I look at example of dropdown menu here . Here is simplified jsfiddle example.
I've got 3 tables that are something like this (simplified here ofc): users user_id
Here is the simplified table structure of PRICES : SKU PriceType FromDate ToDate Price
Here is a simplified version of what I'm trying to do through an expression.
Here is a simplified version of what I have (not working): prog.h: ... const
This is an under-performing query that I've been trying to simplify but am at
i am not sure the best way to go about this. here is the
I'm trying to animate a circle being drawn - here's a simplified version of
EDIT: My problem simplified I suppose has boiled down to not being able to

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.