1

I'm creating chat ( just for two people to chat, not "global" ) and my tables look like this:

MESSAGES:

id | author | receiver | content | sent

where author and receiver are id's from USERS:

id | login | avatar

and i want to get login and avatar both from receiver and author of the message. I was trying with something like this:

SELECT * FROM 
((SELECT messages.*,users.login as starter,users.avatar FROM messages
    LEFT JOIN users ON messages.author = users.id
    WHERE messages.receiver = 1)
UNION 
(SELECT messages.*,users.login as test,users.avatar FROM messages
    LEFT JOIN users ON messages.receiver = users.id
    WHERE messages.author = 1)
    ORDER BY id DESC LIMIT 5) tmp 
ORDER BY id ASC

and few other queries but i could only get one login ( either receiver or author ). Is there any way to do that?

mmmm
  • 3,768
  • 9
  • 36
  • 63

1 Answers1

1

Try this:

select * from messages m
join users a on (m.author=a.id)
join users r on (m.reciever=r.id)

Of course, you can add a where clause to filter it for a specific message, author or whatever.

geomagas
  • 3,230
  • 1
  • 17
  • 27