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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:31:16+00:00 2026-05-23T07:31:16+00:00

I know this question has been asked many times, but I am having trouble

  • 0

I know this question has been asked many times, but I am having trouble implementing it. MySQL isn’t my greatest strength here.

I’ve built an email system for my site and what I want to do is when a user views their inbox they see their messages, however they see the latest sender, date and user details for that particular message instead of the original sender, date and user details.

My table structure as follows:

Message_Header:

-----------------------
messageid    INT
type         VARCHAR
subject      VARCHAR

Message_Recipient:

-----------------------
id           INT
messageid    INT
userid       INT
isread       ENUM
isspam       ENUM
isdelete     ENUM

Message_Detail:

--------------------
dtlid        INT
messageid    INT
from_userid  INT
hash         VARCHAR
type         SMALLINT
body         TEXT
title        VARCHAR
subtitle     VARCHAR
content      TEXT
thumb        VARCHAR
url          TEXT
images       VARCHAR
time_sent    DATETIME
timestamp    INT

I thought something like this would work:

select mh.messageid, mh.subject, mh.type, mr.isread, msg_dtl.from_userid, msg_dtl.firstname, msg_dtl.lastname, msg_dtl.gender, msg_dtl.avatar, msg_dtl.hash, msg_dtl.body, msg_dtl.time_sent
from message_header mh
inner join message_recipient mr on mr.messageid = mh.messageid  
inner join (
    select md.messageid, md.from_userid, u.firstname, u.lastname, u.gender, u.avatar, md.hash, md.body, md.time_sent
    from message_detail md
    inner join users u on u.userid = md.from_userid
    order by md.time_sent desc
    limit 1
) as msg_dtl ON mh.messageid = msg_dtl.messageid
where mr.userid = 5
and mr.isspam = '0'
and mr.isdelete = '0'

But apparently it limits the overall query to just 1 row!

I’ve looked around extensively and I’ve cobbled this together, but I’m worried its not optimised and will be a hog:

SELECT msg_dtl.type, msg_dtl.subject, msg_dtl.isread, u.firstname, u.lastname, u.gender, u.avatar, md.body, md.timestamp 
FROM message_detail md
INNER JOIN users u on u.userid = md.from_userid     
INNER JOIN
(
    SELECT mr.userid, mh.type, mh.subject, mr.isspam, mr.isdelete, mr.isread, md.messageid, md.body, max(timestamp) as timestamp
    FROM message_detail md
    inner join message_header mh on mh.messageid = md.messageid
    inner join message_recipient mr on mr.messageid = mh.messageid  
    GROUP BY md.messageid
) as msg_dtl
USING (timestamp, messageid)
where msg_dtl.userid = 5
and msg_dtl.isspam = '0'
and msg_dtl.isdelete = '0'

I would appreciate any pointers or someone optimising this. if you need any more info please let me know!

  • 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-23T07:31:17+00:00Added an answer on May 23, 2026 at 7:31 am

    This might help with one exception… the IsRead flag.

    First, I’m starting with a “PreQuery” to get based on a single Message (joined to the detail table), the most recent Sequence within the message (via max( mr.id) ), AND the latest DETAIL entry created (assuming auto-incrementing details via Max( md.DtlID ) ) for only those records for the given user that are not spam and not deleted. So this gives us at MOST, one record per message ID.

    That being said, we can now do a direct join back to the header on the message ID 1:1 join

    Then, 1:1 join BACK TO the message recipient on the most recent sequence received… if a chain message is back and forth 10+ times, here is where I assumed you would have intended the most recent email RECEIVED associated WITH the message ID. So, THIS was the record we are getting the “IsRead” flag… even though someone could read a more recent, yet never read a prior in an email chain.

    Next, 1:1 join to the MESSAGE DETAIL table (via Max( md.DtlID ) ) for the last message detail FOR the given message. We don’t have to join on the Message ID since we HAVE the DETAIL ID of the message directly…

    Finally, our detail can then join to the users table to get the “From” user information.

    Hope this helps, including the clarification on how to derive the result set…

    SELECT STRAIGHT_JOIN
          PreQuery.MessageID,
          mr2.IsRead,
          mh.Type,
          mh.Subject,
          md.from_UserID,
          md.isread,
          md.body, 
          md.timestamp,
          u.firstname, 
          u.lastname, 
          u.gender, 
          u.avatar
       from
          ( SELECT
                  mr.MessageID,
                  MAX( mr.ID ) as LastMessageSequence,
                  MAX( md.DtlID ) as LastDetail
               from
                  Message_Recipient mr
                     join Message_Detail md
                        ON mr.messageid = md.messageid
               where
                      mr.userid = 5
                  and mr.isspam = '0'
                  and mr.isdelete = '0'
               group by
                  mr.MessageID ) PreQuery
    
          JOIN Message_Recipient mr2
             ON PreQuery.LastMessageSequence = mr2.ID
    
          JOIN Message_Header mh
             ON PreQuery.MessageID = mh.MessageID
    
          JOIN Message_Detail md
             ON PreQuery.LastDetail = md.DtlID
             JOIN users u 
                on md.from_userid = u.userid
    
       order by 
          md.timestamp desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked many times, but my problem is a
I know this is a question that has been asked many times before, but
I know this question has been asked many times, but I never saw a
I know this question has been asked and answered many times. But almost all
I know this question has been asked many times but those examples aren't working
I know that this question has been asked many times ( but in different
I know this question has been asked many times before but I can't find
I know this question has been asked so many times before but I couldn't
I know this question has been asked many times, but I have not been
I know this question has been asked many times on SO, but I can't

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.