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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:28:09+00:00 2026-05-13T06:28:09+00:00

Using SQL (MySQL) only I would like to select each of the last child

  • 0

Using SQL (MySQL) only I would like to select each of the last child rows of a parent child relationship where the child rows are ordered by a timestamp.

For example using the tables invoices and invoice_items, I want the newest (ie: most recently timestamped) invoice_items records for each invoice respectively.

--------------------------
|Invoices                |
--------------------------
|invoice_id| other fields|
--------------------------
| 1        | ...         |
--------------------------
| 2        | ...         |
--------------------------
| 3        | ...         |
--------------------------

--------------------------------------------
|Invoice_Items                             |
--------------------------------------------
| id | invoice_id | invoice_item_timestamp |
--------------------------------------------
| 1  | 1            | 2009-12-01 10:00:00  |
--------------------------------------------
| 2  | 1            | 2009-12-01 10:01:00  |
--------------------------------------------
| 3  | 1            | 2009-12-01 10:02:00  |
--------------------------------------------
| 4  | 2            | 2009-12-01 9:00:00   |
--------------------------------------------
| 5  | 3            | 2009-12-02 08:30:00  |
--------------------------------------------
| 6  | 3            | 2009-12-03 08:31:00  |
--------------------------------------------    

What is the best SQL syntax to produce a resultset that would look something like the following table?

-----------------------------------------------------
|invoice_id| invoice_item_id |invoice_item_timestamp|
---------------------------------------------------
| 1        | 3               | 2009-12-01 10:02:00  |
| 2        | 4               | 2009-12-01 09:00:00  |
| 3        | 6               | 2009-12-03 08:31:00  |
-----------------------------------------------------
  • 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-13T06:28:10+00:00Added an answer on May 13, 2026 at 6:28 am
    SELECT i.*,it.* 
    FROM invoices i
    INNER JOIN (
     SELECT invoice_id, MAX(invoice_item_timestamp) 
     FROM invoice_items
     GROUP BY invoice_id
    ) it ON (i.invoice_id=it.invoice_id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd just like to verify if using prepared statements in MySQL prevents SQL injection.
I pull markers' data from a MYSQL database using SQL and build up an
I am trying to copy data from my MYSQL table to SQL Server using
Although I am using mySQL (for now), I dont want any DB specific SQL.
I'm migrating a database from MySQL 5 to SQL Server 2008. I'm using the
I'm using Hibernate with JPA and MySQL. I got accessing denied. java.sql.SQLException: Access denied
I would like to search for a particular pattern in my SQL , so
Currently, this is how I perform a query using node-mysql client.query( sql, function( error,
I'd like to find the hits and misses in a table using only a
I would like to transfer a SQL table (let say i. 2 columns :

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.