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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:22:15+00:00 2026-05-28T04:22:15+00:00

I have a simple messaging system, and I’d like to display both Received and

  • 0

I have a simple messaging system, and I’d like to display both Received and Sent messages. The relevant tables/columns are laid out as follows, with extraneous information omitted:

User:

id | firstName | lastName 
-------------------------

Message:

id | fromid | toid | message
----------------------------

The query I’m running is:

SELECT message.id, `fromid`, `toid`, `sent`, `message`, message.email, `read`, user.id, `firstName`, `lastName`, `rank`   
FROM message LEFT JOIN `user` ON message.fromid = user.id OR message.toid = user.id  
WHERE `toid` = '$id' OR `fromid` = '$id'  
ORDER BY `read` DESC, `sent` DESC";

(And for sorting in my PHP script, I do:)

$received = array();  
$sent = array();

while ($dbRow = mysql_fetch_array($query)) {
    if ($dbRow['toid'] == $id) {
        $received[] = $dbRow;
    } else if ($dbRow['fromid'] == $id) {
        $sent[] = $dbRow;
    }
}

So what I want to do select the messages where either toid or fromid are equal to the user’s id, then sort them accordingly (i.e., if the user’s id is in the toid column, it was sent from other user to them, whereas if it’s in the fromid column, the user had sent it to somebody else).

The query almost kind of works, except the $sent[] array contains duplicates: it lists each row twice, once displaying the user’s name and the other displays the receiver’s name. I can kind of see why, but I don’t know what to do to fix it.

I don’t know if I need to adjust my SQL query or my PHP script, so I’ve included both.

I know I could just break it up into two queries, but I’m trying to improve on my SQL and learn more about how JOINs work.

  • 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-28T04:22:16+00:00Added an answer on May 28, 2026 at 4:22 am

    Do two joins – in other words, join the user table one for the “from” and another time for “to”, something like this:

    SELECT message.id, `fromid`, `toid`, `sent`, `message`, message.email, `read`, fromuser.*, touser.*
    FROM message LEFT JOIN `user` fromuser ON message.fromid = user.id 
    LEFT JOIN `user` touser ON message.toid = user.id  
    WHERE `toid` = '$id' OR `fromid` = '$id'  
    ORDER BY `read` DESC, `sent` DESC";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple messaging system on my site. I would like to implement
I have a simple messaging system in Rails with a Message table for the
I have built a very simple internal messaging system inside my application, i want
I have written a simple messaging system for a CMS I am developing and
I have a simple messaging system that up till now uses file based storage.
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script
I have a simple messaging schema where each message has a senderFK and a
I have three different users tables, and I would like to know what is
I want to create a simple Messaging System on my LAMP site. Not an
I am building a simple messaging system with Rails3 that allows users to send

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.