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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:42:21+00:00 2026-05-29T15:42:21+00:00

Code Currently my code looks like that. $stmt = $this->db->prepare(SELECT m.id, m.from_id, m.to_id, m.subject,

  • 0

Code

Currently my code looks like that.

            $stmt = $this->db->prepare("SELECT m.id, m.from_id, m.to_id, m.subject, m.date, m.deleted, m.read, u.fname, u.mname, u.lname FROM msghistory AS m,users AS u WHERE m.from_id=u.id AND m.to_id=u.id AND GROUP BY m.id DESC");
            $stmt->execute();
            $stmt->store_result();
            if ($stmt->affected_rows > 0) {
                $msg = array();
                $stmt->bind_result($msg['id'], $msg['from_name'], $msg['to_name'], $msg['subject'], $msg['message'], $msg['date'], $msg['deleted'], $msg['read']);

                while ($stmt->fetch()) {
                    <echoing results one by one>                    }
            }

What I want to do

I want to get u.fname+u.mname+u.lname (Where msghistory.from_id=users.id) as $msg['from_name'], again u.fname+u.mname+u.lname (BUT this time where msghistory.to_id=users.id) as $msg[‘to_name’].

Example

message | from_id | to-id
hi      | 1       | 5

As you see, this means, user 1 send to user 5 message hi.

Lets say in users table fname, lname for user id=1 – John Doe and for 5 – George Smith
I want to show this as output result

message | from_id | to-id
hi      | John Doe| George Smith

Question

I know that it’s possible with at least 3 queries. But trying to find optimal way. So,is that possible with one and only query?

Update

Based on 2 usefull answers by users I modified query into this

            $stmt = $this->db->prepare("SELECT 
                message.id, message.from_id, message.to_id, message.subject, 
                message.date, message.deleted, message.read, 
                CONCAT_WS(' ',sender.fname, sender.mname, sender.lname) AS sender_name, 
                CONCAT_WS(' ',recipient.fname, recipient.mname, recipient.lname) AS recipient_name,  
                FROM msghistory AS message 
                LEFT JOIN users AS sender ON sender.id=message.from_id, 
                LEFT JOIN users AS recipient ON recipient.id=message.to_id 
                GROUP BY message.id DESC");
  • 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-29T15:42:23+00:00Added an answer on May 29, 2026 at 3:42 pm

    Using explicit joins and clear, readable aliases makes it both a more robust query and easier to read. Try the following:

    SELECT message.*, recipient.*, sender.*
    FROM msghistory AS message
    INNER JOIN users AS recipient ON recipient.id=m.to_id
    INNER JOIN users AS sender ON sender.id=m.from_id
    

    You can replace the select *s with just the fields you want for efficiency

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

Sidebar

Related Questions

I've got some code that currently looks like this (simplified) /* instance in global
My code currently looks like this: private Foo myFoo; public Foo CurrentFoo { get
My code currently looks like this: <div style=position: fixed; width: 35.25%; height: 6.75%; left:
Basic C++ class question: I have simple code currently that looks like something like
My code currently looks like this <% if (request != null) { bustOut; }
My code currently looks like this numbers = [1, 2, 3, 4, 5] def
This is what my code currently looks like: private boolean[] isInitialized = new boolean[COUNT];
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
I'm trying to rotate a camera image. The code looks like that: Bitmap result
I have a dataset that looks like this: 0 _ _ 23.0186E-03 10 _

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.