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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:21:50+00:00 2026-05-30T15:21:50+00:00

I have two entities with an 1-N relation, like this : table_a ——- id

  • 0

I have two entities with an 1-N relation, like this :

table_a
-------
id
name

table_b
------
id
table_a_id
name
status
created_at  

I’m looking for a way in MySQL and especially with Doctrine ORM to query table_a with a “where” clause on table_b that affect only the last associated table_b record.

Supposing I have the following records :

table_a
----------------------------
id    | name
----------------------------
1     | john
2     | mary
3     | chuck

table_b
--------------------------------------------------
id    | table_a_id | name    | status | created_at
--------------------------------------------------
1     | 1          | blue    | 1      | 2000-01-01
2     | 1          | red     | 1      | 2012-12-31
3     | 2          | yellow  | 1      | 2000-01-01
4     | 2          | green   | 0      | 2012-12-31

So I want to tell MySQL/Doctrine :
GIVE ME the table_a records
WHICH HAVE table_b records
AND status = 1 ON the last related elements (according to the created_at field)

This should only return :

table_a
----------------------------
id    | name
----------------------------
1     | john
  • 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-30T15:21:52+00:00Added an answer on May 30, 2026 at 3:21 pm

    According to the book SQL Antipatterns, this type of join with the proper indexes can often perform better than a subquery. So, try this method out too:

    SELECT a.*
    FROM table_a a
    JOIN table_b b1
      ON b1.table_a_id = a.id
      AND b1.status = 1
    LEFT JOIN table_b b2
      ON b2.table_a_id = a.id
      AND b2.created_at > b1.created_at
    WHERE b2.table_a_id IS NULL
    

    If there could be two rows from table_b with status 1 that have the same table_a_id and created_at date, then you will need DISTINCT to avoid duplicates:

    SELECT DISTINCT a.*
    FROM table_a a
    JOIN table_b b1
      ON b1.table_a_id = a.id
      AND b1.status = 1
    LEFT JOIN table_b b2
      ON b2.table_a_id = a.id
      AND b2.created_at > b1.created_at
    WHERE b2.table_a_id IS NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Doctrine entities that have a one-to-many relationship, like this: License class
Let's say, I have two tables, entities and tags. Relation between them is expressed
I have two entities, A and B. A has to-Many relation with B. One
I have two entities: product and tag connected by many-to-many relation. Here is a
I have two entities like the following: @Entity public class Trip { @OneToMany(mappedBy =
I have two entities: patient and checkpoint. Patient has attributes such as DOB, name,
I have two entities A and B that are related in a MxN relation
hi i have two entities User and Authority they have many to many relation:
When i have a relation between two entities in my model: [GroupMember] (*) -----
I have two entities Foo and Bar with a Many to Many relationship between

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.