Get the last message from each conversation

Skyey

I have table for messages which contains id, sender_id, receiver_id, message and conversation_id

(I connected them by conversation_id, which they make by sending first message and if someone is replying, first he search conversation_id from messages where he is receiver and guy where is he replying is sender and by that send message with same conversation_id)

Now in messages list I want to output one just one last row per different conversation_id where sender_id='$my_id' OR receiver_id='$my_id'

I am using DISTINCT but I get all rows always as output:

SELECT DISTINCT conversation_id, sender_id, message 
FROM messages 
WHERE receiver_id='$my_id' 
ORDER BY id DESC
1000111

Please give the following query a try:

SELECT 
M.*
FROM messages M
INNER JOIN 
(
    SELECT 
    MAX(id) AS last_id_of_conversation,
    conversation_id
    FROM messages
    GROUP BY conversation_id
) AS t
ON M.id = last_id_of_conversation

Explanation:

SELECT 
 MAX(id) AS last_id_of_conversation,
 conversation_id
FROM messages
GROUP BY conversation_id;

This inner query will generate an output where there will be one row for each conversation_id along with the last id value (or max id) of the conversation.

Later make an inner join between your main table (messages) and the result returned by the inner query on matching id. I hope id is primary key. If so, then the above query ensures to bring a single row (more specifically the row having the last message) for each conversation.

EDIT:

SELECT 
M.*
FROM messages M
INNER JOIN 
(
    SELECT 
    MAX(id) AS last_id_of_conversation,
    conversation_id
    FROM messages
    WHERE sender_id = ? OR receiver_id = ?
    GROUP BY conversation_id
) AS t
ON M.id = last_id_of_conversation

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get last message from each conversation

From Dev

SQL - Get last message from each conversation

From Dev

Get Last Message from Each Conversation sql

From Dev

Get Last Message from Each Conversation sql

From Dev

mySQL select the last message from each conversation

From Dev

Get last Message Reply From Conversation

From Dev

Select last message of each conversation

From Dev

Get the last message for each thread

From Dev

How to get the latest message in each conversation of a certain user in SQL?

From Dev

SQL - Last conversation Message list

From Dev

Get latest message in conversation

From Dev

Mongoose - find last message from each user

From Dev

Get id of recipient of each conversation

From Dev

Get last entry from each user in database

From Dev

How to get the last row from each users?

From Dev

Get last record from each different id

From Dev

Get last message from kafka consumer console script

From Dev

Get last message from text channel with discord.js

From Dev

How to get the last 5-10 message from a database?

From Dev

Get last document from userchat for each user - mongoose

From Dev

Get last document from userchat for each user - mongoose

From Dev

Bash Command to Get First And Last File from Each Folder Recursive

From Dev

Postgresql - How to get value from last record of each month

From Dev

Pandas - From list of dates, get the last date in each month

From Dev

Thunderbird forward only most recent message from conversation

From Dev

Get the last record from each month, from each MATERIAL id after a rolling sum

From Dev

Get last message of Observable with RxJS

From Dev

MySQL get last Message (VarChar)

From Dev

Get last record of each ID

Related Related

HotTag

Archive