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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:33:18+00:00 2026-06-16T16:33:18+00:00

Say I have three tables: users with names IDs emails containing rows which have

  • 0

Say I have three tables:

  • users with names IDs
  • emails containing rows which have their email address
  • messages containing rows which contain the dates and titles of their emails

the second and third tables can all be matched to the first using the user’s ID of course.

I want a query which will return each user once only, their email address and the date of their most recent message.

If I use this:

SELECT name,email,title,date
FROM users
JOIN emails ON users.id = emails.user_id
JOIN messages ON messages.user_id = emails.user_id
group by name
order by date desc

I don’t get the most recent message, because the ordering has happened after the joining and the grouping. I get one email each from my users, and the emails are sorted by their date.

Can this be done in one join? What am I missing?

Pastebin for a dummy database you can use: http://pastebin.com/1x273aEe — the actual database is not exactly like this, but it’s the same problem.

  • 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-16T16:33:19+00:00Added an answer on June 16, 2026 at 4:33 pm
    SELECT  a.*, b.*, c.*
    FROM    users a
            INNER JOIN emails b
                ON a.ID = b.user_ID
            INNER JOIN messages c
                ON a.ID = c.user_ID
            INNER JOIN
            (
                SELECT user_id, MAX(date) maxDate
                FROM messages
                GROUP BY user_ID
            ) d ON c.user_ID = d.user_ID AND
                    c.date = d.maxDate
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have three tables, a table of users, a table of around 500
Let's say I have three tables (with the columns in each): USERS: id, name,
let's say I have three tables (MySQL), one for users, one for tags and
Say I have three tables: Fruit (Table 1) ------ Apple Orange Pear Banana Produce
Let's say you have three tables named Item, Event, and Seat, constructed as such:
Lets say that I have three tables, customers, orders and orderDetails. I'm doing this:
Lets say I have three models/tables: operating_systems , words , and programming_languages : #
I have say three tables. Projects Id Name Categories Id Name ProjectCategories Id ProjectId
i have a problem with joining three tables in mysql. lets say we have
I have three tables say 'user', 'user_resources' and 'desktop_resources'. 'user' contains - emp_id,name and

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.