This is my MySQL DB structure:
Recipents (id, converstation_id, user_id)
Conversations (id, user_start)
Messages (id, user_id, message, conversation_id, time)
What I am trying to do is to pull all the conversations ids that were started by a specific user or he is the recipient of one of the conversations. And sort it by the last message.
How can I do that?
The following query will work. You’ll have to replace
REQUESTED_USER_IDby a valid user id. Note the UNION statement in SQL that concats the result of the two sub queries. The result from the UNION is then wrapped in another SELECT to achieve the order by start time.