How to get last messge between two friends in the messge table having id in descending order?
select a.id, a.msg, a.sender, a.receiver, a.friend_id
from msg a
left join msg b on (a.friend_id = b.friend_id and a.id &lt; b.id)
where b.id is null
  and (a.sender = 1 or a.receiver = 1)
order by a.id desc;


Can be tested at http://sqlfiddle.com/#!9/35823/10

It uses "groupwise-max" solution with a left join as can be found for instance in mysql manual (last example).

Your schema seems a bit weird keeping sender, receiver and friend_id, but I suppose friend_id is a FK to some friends table where pairs of (1,2) and (1,6) are stored, so it is more of a "friendship_id" ?