分组依据和mysql排序比显示最新帖子

时间:2013-09-22 15:43:25

标签: php mysql

搜索后我可以找到答案。这是我的问题 这是我试图做的,但没有显示结果中的最新帖子

$result = mysql_query("SELECT * FROM berichten where ontvanger = '$session->username' 
GROUP BY fromm ORDER BY date DESC");


while($row = mysql_fetch_array($result))
 {
echo $row['fromm'];
echo $row['msg'];
echo $Row['date'];
}

我想将表格从atm分组,当我这样做时,我想显示表格中的最新帖子,但显示旧帖子

1 个答案:

答案 0 :(得分:0)

SELECT  a.*
FROM    berichten a
        INNER JOIN
        (
            SELECT  fromm, MAX(Date) Date
            FROM    berichten
            GROUP   BY fromm
        ) b ON a.fromm = b.fromm AND
                a.date = b.date
WHERE   a.ontvanger = '$session->username'

也可以写成

SELECT  a.*
FROM    berichten a
WHERE   a.Date = 
        (
            SELECT  MAX(Date)
            FROM    berichten b
            WHERE   a.fromm = b.fromm
        ) AND
        a.ontvanger = '$session->username'