title: how to find last message from message table and also find users
body: <p>This is my User table
  <a href="https://i.stack.imgur.com/Xsh7j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Xsh7j.png" alt="enter image description here"></a>
  This is my Message table
  <a href="https://i.stack.imgur.com/jyqRg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jyqRg.png" alt="enter image description here"></a></p>
  
  <p>And tHis is my Listing Table
  <a href="https://i.stack.imgur.com/ufRRN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ufRRN.png" alt="enter image description here"></a></p>
  
  <p>Now I get successfully get the last message with this query</p>
  
  <pre><code>enter code here
  $coreQueryUser = $this-&gt;Message-&gt;query('
              select m.*
              from
                  messages m
                  inner join (
                  select max(id) as maxid
                  from messages
                  where messages.list_id = 3 
                  group By (if(sender_id &gt; reciever_id,  sender_id, reciever_id)), 
                  (if(sender_id &gt; reciever_id,  reciever_id, sender_id))
                 ) t1 on m.id=t1.maxid 
          ');
  </code></pre>
  
  <p>when i run this query it gives me following output:-</p>
  
  <pre><code>enter code here
  &lt;pre&gt;Array
  (
  [0] =&gt; Array
      (
          [m] =&gt; Array
              (
                  [id] =&gt; 2
                  [sender_id] =&gt; 10
                  [reciever_id] =&gt; 21
                  [list_id] =&gt; 3
                  [message] =&gt; hello sir
                  [add_date] =&gt; 2016-09-25 08:24:38
                  [is_check] =&gt; 0
              )
  
      )
  
  [1] =&gt; Array
      (
          [m] =&gt; Array
              (
                  [id] =&gt; 7
                  [sender_id] =&gt; 10
                  [reciever_id] =&gt; 22
                  [list_id] =&gt; 3
                  [message] =&gt; hmm
                  [add_date] =&gt; 2016-09-27 00:00:00
                  [is_check] =&gt; 0
              )
  
      )
  )
  </code></pre>
  
  <p>Now you see in the output in first array sender_id =10 and reciver_id  = 21
      and list_id = 3 
  List is created by user whose id is 10. in the output i want to fetch the User data whose user id 21. Remember that when reciever send the message to sender the ids are interchanging. in that case i also want the data of whose user id is 21</p>
  
  <p>Please help me:(</p>