0

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

$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 的最新帖子,但它显示的是旧帖子

4

1 回答 1

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'
于 2013-09-22T15:46:37.787 回答