搜索后,我可以找到答案。这是我的问题
这是我尝试执行的操作,但未显示结果中的最新帖子

$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'];
}


我想对表fromm进行分组,当我这样做时,我想显示表fromm的最新信息,但显示旧信息

最佳答案

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'

关于php - mysql的group by和order by比显示最新的帖子,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18945635/

10-10 18:10