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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:58:26+00:00 2026-05-22T17:58:26+00:00

users tbl: id username first_name last_name email_address messages tbl: id date_sent title content status

  • 0
users tbl:
id
username
first_name
last_name
email_address

messages tbl:
id
date_sent
title
content
status

message_relation tbl:
id
message_id
sender_id
receiver_id

What would be the most efficient way to query a message TO a selected user given these tables? In other words, I want to list all messages that are in userA’s “inbox”

Secondly, how would you recommend handling global messages that need to be sent to everyone from the admin?

  • 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-22T17:58:26+00:00Added an answer on May 22, 2026 at 5:58 pm

    I think this is what you’re looking for..

    SELECT [WHATEVER] FROM 
    messages
    INNER JOIN message_relation
     ON (messages.id = message_relation.message_id AND message_relation.receiver_id = $id)
    

    This does what you asked for efficiently. If you also want to select fields on either of the users, you can JOIN Receiver_ID to User_ID to to get that.

    Suggested tables setup if you want Global/Admin messages:

    Messages (Message_ID, [Fields common to all messages, e.g. Message_Content, Message_Timestamp, etc])
    Global_Messages (Global_Message_ID, Message_ID, [any fields specific to Global_Messages])
    User_Messages (User_Message_ID, Message_ID, [any fields specific to User_Messages])
    User_Message_Relations (User_Message_Relations_ID, User_Message_ID, Sender_ID, Receiver_ID)
    

    Then, to query an Inbox, something like:

    SELECT [WHATEVER] FROM
    Messages
    
    LEFT JOIN (Global_Messages)
     ON (Messages.Message_ID = Global_Messages.Message_ID)
    
    LEFT JOIN (User_Messages)
     ON (Messages.Message_ID = User_Messages.Message_ID)
    
    LEFT JOIN (User_Message_Relations)
     ON (User_Messages.User_Message_ID = User_Message_Relations.User_Message_ID AND User_Message_Relations.Receiver_ID = $uid)
    

    That will give you a result you can loop through to get a user’s entire Inbox.

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

Sidebar

Related Questions

if ($_POST) {$content = stripslashes($_POST['content']); $by = $_SESSION['exp_user']['username']; $dt = date(F j, Y, g:i
My table structure looks like this: tbl.users tbl.issues +--------+-----------+ +---------+------------+-----------+ | userid | real_name
SELECT m.id, m.title, m.message, m.from, m.to2, m.to_viewed, m.from_viewed, m.created, u.username FROM tbl_messages m INNER
I have a table where there are two columns. First_Name and Last_name. I am
here I am trying to remove any users which containt a in their email/username.
I have a table that contains intervals: CREATE TABLE tbl ( user_id: INTEGER, start:
Users will be able to write some documents. Those documents will consists of chapters
Users have subscriptions to feeds but when I change (as suggested elsewhere on stackO)
Users can use their Google, Facebook or Twitter account to login to a site
Users call a web service API in my PHP code but don't properly encode

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.