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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:52:03+00:00 2026-05-23T08:52:03+00:00

I am creating a threaded message system much like gmail and facebook where the

  • 0

I am creating a threaded message system much like gmail and facebook where the inbox lists the most recent threads displaying the subject, the sender’s name, and time-stamp of the most recent message.

Here’s How my tables are set up:

users:
    user_id
    user_name

thread:
    thread_id
    title
    to_id
    to_keep
    to_read
    from_id
    from_keep
    date

message:
    message_id
    thread_id
    to_id
    from_id
    message_text
    date

What I’m doing right now is when a user creates a new message, it creates a new thread in the thread table and then a new message in the message table and if a user responds to a thread, it duplicates the current thread in the thread table except it swaps the to_id and from_id and then creates a new message based on that.

Also, for the inbox view, I’m able to just query all threads based on a user_id. so something like SELECT * FROM thread WHERE to_id = 2 and to_keep = TRUE ORDER BY date DESC or if I want to view messages in the outbox it would be something like SELECT * FROM thread WHERE from_id = 2 and from_keep = TRUE ORDER BY date DESC.

If a user opens a thread when there’s a new message, then to_read is updated to true UPDATE thread SET to_read = TRUE WHERE thread_id = 4.

I feel like I’m over complicating this process and that there should be a better way to do this.

Any help or ideas would be appreciated.

This way let’s me just select everything from the thread table and then do a join with the user table to display everything I need. However I feel like there should be a better way to do this.

  • 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-23T08:52:04+00:00Added an answer on May 23, 2026 at 8:52 am

    Why don’t you separate out message relationships from user’s view of each message?

    I would do the threading by a self-referencing relationship on message. In other words, the message has a “responding_to_message_id” column.

    I’m not sure I understand why you have a “to_id”. Are messages directed to individual users? This seems very limited. I would think that you would either have no recipient (i.e. the recipient is a message board that anyone can read) or you would have the ability to specify multiple recipients, just like with an e-mail. Perhaps you can explain more about how the system is to be used.

    Assuming (for simplicity) that you are posting to a board, so only the “from” is important, then you have your message table, with self-referencing relationship for threading, a user table, and then an intersection table between user and message that stores which messages have been read by each user.

    That way, if you want to know if a user has read a message or not, just attempt to read the user ID in the intersection table for the given message. If it isn’t there, then that message is unread by that user.

    Note that if you want to have single recipients this design holds and if you want to have multiple recipients you can use an intersection table to hold the list of recipients for each message. If you do have a recipient intersection table, it can do double-duty as your read status table.

    EDIT: ERD Sketch:

    Here is a quick sketch of what I’m talking about…

    ERD Sketch

    Whether or not the sender has chosen to keep the message is flagged on the message itself. If the message is the start of a new thread, the reply_to_message_id column is NULL, otherwise it is the message_id of the parent message. There can be mulitple recipients, each of which have their own ability to keep the message or not, as well as the ability to track the date and time when the recipient reads the message.

    EDIT 2: Alternate ERD and Querying for Most Recent Message

    @OP asked how to query for the most recent message in a thread. The answer depends on the form of the thread. You can either have a flat thread where every message goes to the end of the linear stream of messages or you can have a tree-shaped thread where each message has a specific parent, unless it’s the root of the thread. In the ERD above, the reply_to_message_id field could be used either way. If the thread is flat, then the FK is always to the root MESSAGE. If the thread is tree-shaped, then the FK is to the immediate parent of the reply MESSAGE.

    If a typical query you want to run is “what is the most recent message in a thread?” and your threads are flat, then you can use SQL like this:

    select top 1
      M.message_id
    , M.sent_datetime
    , M.title
    , M.message_text
    , S.user_id
    , S.user_name
    -- and anything else you want...
    from MESSAGE M inner join USER S
      on M.sender_user_id = U.user_id
    where M.reply_to_message_id = @ThreadRootMessageID
    order by
      M.sent_datetime desc
    

    If, on the other hand, your threads are tree-shaped and this is a query you want to be able to run quickly and easily, then the schema in the ERD above is not very easy to work with. SQL is not good at trees. You can solve the problem with a little bit of denormalization. See the ERD below:

    Tree Thread ERD

    Note that there is now one FK to show the immediate parent and one FK to show the root. Since threads aren’t subject to editing – at least to edits where the root of a message is changed to point at a different thread, the denormalization that this entails does not imply risk of update anomallies so the redundancy is not too problematic.

    If you use this ERD then the query for “most recent message in thread X” is the same as above, but with M.thread_root_message_id in the where clause instead of M.reply_to_message_id.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a threaded message board and I'm trying to keep it simple. There's
Creating a voting system just like stackoverflow. Questions like this have been asking several
I'am writing a multi-threaded program in C. Before creating the threads, a global python
I am creating a multi-threaded application. However, I have experienced lots of unexpected behavior
Creating a pacman-like game, where a player eats objects. When a MovieClip (box) eats
I'm creating a multi-threaded application in C using Linux. I'm unsure whether I should
I am creating a multi-threaded application in which each thread will appear as a
I am creating a threaded application that uses double buffering and I am trying
I have a multi-threaded Delphi program creating multiple instance of some classes, and I
I'm creating an application that uses .Net and Mono, it uses cross-threaded forms as

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.