sql - sqlite query with second joining -


i have table messages :

enter image description here

holding messages users conversations

each conversation looks this:

enter image description here

now have query i

select m.*  ( select message_conversationid           messages m   m.user_id = 1   group message_conversationid ) mc   join      messages m                                 on  m.message_id =                   ( select mi.message_id                         messages mi             mi.message_conversationid                        = mc.message_conversationid             , mi.user_id = 1             order mi.message_timestamp desc,                        mi.message_id desc                       limit 1                                         )          order m.message_timestamp desc 
  • which returns me list of last message each conversation
  • **last defined last message_timestamp & last message_id

enter image description here

every row in messages table holds info 1 side of conversation sends message eg. me or other person

when query conversations i need know second side of conversation need add query ii select / join statement

left join   (  select message_conversationid, message_from message_from2, message_avatar_pic_id message_avatar_pic_id_from2     messages  msg_from         -- not me - second side selected in prev query !!! ---      message_from <> 'me' collate nocase      , user_id == 1     group message_conversationid   ) not_me on message_conversationid = not_me.message_conversationid 

to have result:

enter image description here

how combine 2 queries + ii ?

edit:

  • i want left joining because there no second side of conversation - example message send or received !


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -