Get Last Message from Each Conversation sql

Can1

I have a sql table which include conversation between users. I need to retrieve the last message from every conversation in order to preview it.

id | sender   | receiver     | message      | date
 1 |        1 |            2 | Hello        | 2015-12-08 20:00
 2 |        2 |            1 | Hey          | 2015-12-08 20:10
 3 |        2 |            1 | You there?   | 2015-12-08 21:00
 4 |        1 |            3 | Yes          | 2015-12-08 21:15
 5 |        4 |            1 | Hey buddy    | 2015-12-08 22:00

I know many similar question on site but i couldn't fix this.

I tried this code but not working properly:

SELECT *
FROM   messages
WHERE  receiver = '{$id}'
GROUP BY sender
ORDER BY id DESC
LIMIT 10;
RJardines

Just to define what is a conversation in your table is a pain, i suppose a conversation is all the rows where

(sender=@senderId && receiver=@receiverId) || (sender=@receiverId && receiver=@senderId)

Group by this concept, i don't even want to think it

For me you are missing a concept, the "conversation"

If you have a table conversation like this

ConversationId |  Users1  |  User2

And Message like

Id | ConversationId | UserSendingId | Message | Date

Now you can Group by ConversationId and take the last message like

SELECT *  <-- avoid * better use all row names
FROM Message 
Where id in (
 select max(id) from message group by ConversationId
)

The representation of the conversation table is just a fast approach you can do a better solution with a relation from 1 to many of conversation and users in conversation to avoid modified conversation table when you want to have more than 2 users per 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

SQL - Get last message from each conversation

From Dev

Get Last Message from Each Conversation sql

From Dev

Get last message from each conversation

From Dev

Get the last message from each conversation

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

SQL - Last conversation Message list

From Dev

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

From Dev

Get the last message for each thread

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

return last message from grouped messages SQL

From Dev

SQL get first and last transaction for each user

From Dev

Sql for get last 3 rows for each level

From Dev

Get the last set of dates in each year in sql?

From Dev

Sql for get last 3 rows for each level

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 first and last date time from each date in query SQL Server

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 cell in a column from an SQL

From Dev

Get the last entered id from a table in sql

From Dev

Get last cell in a column from an SQL

From Dev

Get data from last month in SQL Server