0

I am building an in app Chat, I've figured out the DB tables on how to store the chat messages, having only private chat. Now I am trying to come up with a good SQL Query for showing all conversations for user with a certain ID alongside with user's name and profile picture. It seems like I need to use multiple different WHERE clauses for different columns I want to get returned. How do I achieve the wanted result?

My tables are:

enter image description here

I so far have something like this:

SELECT
    chat_message.from_user_id,
    chat_message. `message_text`,
    chat_message.sent_datetime,
    chat_conversation.is_read,
    chat_conversation.id AS conversation_id,
    user_profile.`name`
FROM
    chat_conversation
    LEFT JOIN chat_message ON chat_conversation.id = chat_message.conversation_id
    LEFT JOIN chat_participant ON chat_conversation.id = chat_participant.conversation_id
    LEFT JOIN user_profile ON chat_participant.user_id = user_profile.user_id
WHERE
    chat_participant.user_id = UUID_TO_BIN("62C340CE262B4DE49E2A99B3B3126BB6")
AND chat_message.id in(
    SELECT
        max(chat_message.id)
        FROM chat_message
    GROUP BY
        conversation_id)

But with this query, I will always get only my own Name and My own profile picture.

kalafun
  • 3,512
  • 6
  • 35
  • 49

0 Answers0