Selecting multiple values from two table at once
15:47 13 Feb 2012

Code

Currently my code looks like that.

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

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.

Let's 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?

mysql