SELECT count(`read` = 0 or null) AS newMessage, receiver,content,newTable.inputTime, u.name, u.img FROM (SELECT receiver_id as receiver,content,`read`,inputTime FROM chat WHERE (sender_id = 91) AND (receiver_id <> 91) UNION SELECT sender_id as receiver,content,`read`,inputTime FROM chat WHERE (sender_id <> 91) AND (receiver_id = 91) ORDER BY inputTime DESC) as newTable INNER JOIN user u on receiver = u.id GROUP BY receiver ORDER BY inputTime DESC LIMIT 0, 10