mysql - Get biggest value of group after join -


i have 2 tables in db:

conversation:

conversationid | persone | perstwo | timestamp 1              |    1    |  3      | 1431680294000 2              |    3    |  8      | 1431680407000 

message:

messageid | conversationid | senderid | receiverid | message | timestamp     |  seen 1           1                1          3            xyz!      1431680294000    0 2           2                3          8            hi x!     1431680405000    0 3           2                3          8            allt bra? 1431680407000    0 

now want find latest message of each conversation.

my approach right find conversations of user , try group conversationid:

select distinct conversationid, senderid, receiverid, message, timestamp  message conversationid in       (select conversationid conversation persone = 3 or perstwo = 3) group conversationid order timestamp desc 

unfortunately these results:

2   3   8   hi x!   1431680405000 1   1   3   xyz!    1431680294000 

even though need allt bra? result last conversation.

i running newest version available arch of mariadb.

a common pattern type of question find last item in derived table , join filter main query. in case query like:

select *  conversation c join message m on c.conversationid = m.conversationid join (   select conversationid, max(timestamp) timestamp    message   group conversationid ) x on m.conversationid = x.conversationid    , x.timestamp = m.timestamp c.persone = 3 or c.perstwo = 3 

sample sql fiddle


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -