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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:43:48+00:00 2026-06-12T09:43:48+00:00

So I have two tables like this: create table A { id int; …

  • 0

So I have two tables like this:

create table A
{
    id int;
    ...
}

create table B
{
    id int;
    a_id int;
    t timestamp;
    ...
}

A is one-to-many with B

I want to:

SELECT * FROM A LEFT JOIN B ON A.id = B.a_id ???

But I want to return exactly one row for each entry in A which has the B with the newest t field (or null for Bs fields if it has no B entry).

That is rather than returning all A-B pairs, I want to only select the newest one with respect to A (or A-null if no B entry).

Is there some way to express this in SQL? (I’m using MySQL 5.5)

  • 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-12T09:43:49+00:00Added an answer on June 12, 2026 at 9:43 am

    LEFT JOIN is only concerned with ensuring every row in A is returned, even if there is no corresponding joined row in B.

    The need for just one row needs another condition. MySQL is limitted in its options, but one could be:

    SELECT
      *
    FROM
      A
    LEFT JOIN
      B
        ON  B.id = A.id
        AND B.t = (SELECT MAX(lookup.t) FROM B AS lookup WHERE lookup.id = A.id)
    

    Another could be…

    SELECT
      *
    FROM
      A
    LEFT JOIN
    (
      SELECT id, MAX(t) AS t FROM B GROUP BY id
    )
      AS lookup
        ON lookup.id = A.id
    LEFT JOIN
      B
        ON  B.id = lookup.id
        AND B.t  = lookup.t
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables that look like this: CREATE TABLE table1 (user_id int, the_date
I have two tables like this: CREATE TABLE `tblFacilityHrs` ( `id` int(11) NOT NULL
I have many similar structure tables like this: CREATE TABLE [dbo].[tbl_Hierarchy]( [ID] [int] NOT
I have two tables which looks something like this Table Queue int ID; string
I have two tables like, CREATE TABLE [dbo].[entry] ( [id] [int] NULL, [service] [int]
I have two tables like these: CREATE TABLE people ( id INT NOT NULL,
I have two tables, A and B. The structure looks like this: CREATE TABLE
I have two tables in my database that look like this CREATE TABLE IF
I have create two tables with reference with another table: I like this: Table1:
I have two tables kinda like this: // Person.Details Table: PersonID int [PK] |

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.