Suppose there are two tables `chat` and `users`. `chat` have four columns id, sender, receiver and message and `users` have two columns id and name.
chat:
id | sender | receiver | message |
1 | 1 | 2 | Hi, how are you? |
2 | 2 | 1 | I'm fine |
users:
id | name |
1 | ABC |
2 | BCD |
SELECT u1.name as sender, u2.name as receiver, c.message FROM `chat` as c
LEFT JOIN `users` as u1 ON c.sender = u1.id
LEFT JOIN `users` as u2 ON c.receiver = u2.id
In the result you will get sender name, receiver name and message/chat that taken place between them.