S
S
Sukhrob Saidov2017-05-23 08:02:17
MySQL
Sukhrob Saidov, 2017-05-23 08:02:17

How to display correspondence in pairs?

Hello!
There is a table " messages " (id, from_id, to_id, message, read, timestamps)
And I need to display the correspondence between users by pair.
My query turns out like this:

Message:where('from_id', $id)->orWhere('to_id', $id)->get();//получаем кому он отправлял и от кого получал

But I can’t group them, for example: the
1st user wrote to the 2nd user , the 2nd user replied to the 1st
In this situation, their roles change, i.e. (from_id = 1st user, to_id = 2nd user) and (from_id = 2nd user, to_id = 1st user)
My request is to display each record separately, but I need to get incoming and outgoing messages in pairs between the recipient and the sender.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2017-05-23
@1ax3l

SELECT * FROM message WHERE (from=1 AND to=2) OR (from=2 AND to=1)

What is in pairs? If the first user sent the second three messages in a row, and the second did not answer, then everything will break?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question