Get last message from each conversation

Camilo

I know similar questions had been asked before, but none of them had this same conditions and their answers didn't work for this case.

The table containing the messages looks like this:

id | owner_id | recipient_id | content      | created
 1 |        1 |            2 | Hello        | 2015-12-08 20:00
 2 |        2 |            1 | Hey          | 2015-12-08 20:10
 3 |        3 |            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

And let's say I query for the last message from each one of the conversations for User ID 1, the expected result is:

id | owner_id | recipient_id | content      | created
 5 |        4 |            1 | Hey buddy    | 2015-12-08 22:00
 4 |        1 |            3 | Yes          | 2015-12-08 21:15
 2 |        2 |            1 | Hey          | 2015-12-08 20:10

I tried many combinations, using JOINs and sub-queries but none of them gave the expected results.

Here is one of the queries I tried but it's not working. I believe is not even near to what I'm needing.

SELECT
    IF ( owner_id = 1, recipient_id, owner_id ) AS Recipient,

    (
        SELECT
            content
        FROM
            messages

        WHERE
            ( owner_id = 1 AND recipient_id = Recipient  )
        OR
            ( owner_id = Recipient AND recipient_id = 1 )

        ORDER BY
            created DESC

        LIMIT 1
    )
FROM
    messages

WHERE
    owner_id = 1
OR
    recipient_id = 1

GROUP BY
    Recipient;
splash58
select t.* 
    from 
        t 
      join 
        (select user, max(created) m  
            from 
               (
                 (select id, recipient_id user, created 
                   from t 
                   where owner_id=1 ) 
               union 
                 (select id, owner_id user, created
                   from t 
                   where recipient_id=1)
                ) t1
           group by user) t2
     on ((owner_id=1 and recipient_id=user) or 
         (owner_id=user and recipient_id=1)) and 
         (created = m)
   order by created desc

example on sqlfiddle

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 sql

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

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