$sql = mysql_query("select * from (SELECT * FROM `forum` where `type`='m' order by `id` desc) as `msg` group by `refid` order by `id` desc limit 10"); echo '<pre>'; while($row = mysql_fetch_assoc($sql)) { print_r($row); }