2013-09-22 76 views
0

搜索後我能找到答案。這是我的問題 這就是我試圖做的,但沒有出現在結果按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']; 
} 

我想組表弗洛姆,當我做到這一點我想表明表弗洛姆但其顯示的最新帖子最新的帖子老後

回答

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' 
相關問題