In mysql there is a table: id | sender | receiver | message_text | read | created_at id | sender | receiver | message_text | read | created_at id | sender | receiver | message_text | read | created_at .

The user can be sender - the sender of the message or receiver - the recipient of the message. I need to get IDs of all interlocutors with whom the user was charged, being in any of the designated roles.

For example:

 id | sender | receiver | message_text | read | created_at -------------------------------------------------------------------- 1 | 3 | 6 | abc demo text | 0 | 2016-02-02 10:20:20 2 | 1 | 8 | abc demo text | 0 | 2016-02-02 12:20:20 3 | 6 | 3 | abc demo text | 0 | 2016-02-03 12:10:20 4 | 6 | 8 | abc demo text | 0 | 2016-02-03 12:10:33 

The user with id=6 received or sent messages to users with id=3 and with id=8 . At the exit, I want to get the answer 3,8 . What should the query look like for this?

    1 answer 1

    You need to cling to something, for example, you have a condition:

    The user with id = 6 received or sent messages to users with id = 3 and with id = 8.

     SELECT `id` FROM `table` WHERE `sender` = '6' OR `receiver` = '6'; 

    We get: 1,3,4. As stated in the condition

    get IDs of all interlocutors with whom the user was charged, being in any of the designated roles

    If I am not right - correct, maybe the condition has not reached me right

    • one
      Yes, this is absolutely the right answer. from fatigue did not realize what I was doing wrong and got into case / if - J. Doe