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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:50:50+00:00 2026-06-09T13:50:50+00:00

Assuming I have two tables, users and audit, and the audit table is populated

  • 0

Assuming I have two tables, “users” and “audit”, and the audit table is populated by a trigger each time the user’s status changes. I’m trying to do a select from both tables, and get the user entry (unique) with the associated latest audit entry. Say the tables have the following fields:

users:

  • id
  • username
  • email
  • status

audit:

  • id (just an arbitrary autoincrement)
  • user_id (fk)
  • new_status
  • created_at (date/time when this happened)

Thus, for each user entry, there’s multiple audit entries.

So, eventually I need the result to be say (where the italic part is the results from audit, and the non-italic is the user part of the row in the mysql result-set):
1, john, john@gmail.com, 10, 5, 1, 10, 2012-08-10-10:15:59
etc

Now, the only way I can see is to do compounded selects, where I do something like:
SELECT U.*, (SELECT * FROM audit A2 where A2.id = max(A.id)) FROM users U left join audit A on (U.id = A.user_id);

Problem is, if you’re having thousands or perhaps hundreds of thousands of results in user, you’ll be doing n+1 queries internally in the DB – which is really inefficient.

Any ideas on how to write this as one SQL statement without using compounded SELECTS?

  • 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-09T13:50:51+00:00Added an answer on June 9, 2026 at 1:50 pm

    I am assuming there is always a corresponding audit entry, and have thus used INNER JOIN. Also assuming there are not two audit entries with the same created_at date for the same user.

    select u.id, u.username, u.email, u.status, 
        a.id, a.new_status, a.created_at
    from users u
    inner join (
        select user_id, max(created_at) as MaxCreated
        from audit
        group by user_id
    ) am on u.id = am.user_id
    inner join audit a on am.user_id = a.user_id and am.MaxCreated = a.created_at
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have two SQL tables consisting of a single column, i.e. Table 1
I have a SQL database with two tables like this: Users Id (PK) Name
I have two tables that look like following: RELEASE TABLE name asin MATCHES TABLE
I have a query that performs a UNION between two tables. I'm trying to
Let's say I have two tables: Report Comment And assuming I have a database
I'm kinda stuck defining a Hibernate Entity: Assuming I have the following two tables
I have two tables: A table of restaurants and a table of areas. The
If I have two tables: Users and Appointments. How would I query the db
I have two tables in the system. I'm trying to make a VIEW that
I have two MySQL tables like this: User Id (PK, auto-increment integer) Subscriber Id

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.