0

我正在开展一个教育项目,为 IOS 应用程序创建一个简化的消息传递系统。并且在排序我的消息列表时遇到问题(不是实际的对话列表)

表:消息 - FIELDS = id, touser, from user, date, message, photo, readmessage。当前代码:

 <?php
 $enduser = $_GET['username'];
 $db = b2fdev;
 $con = mysql_connect("localhost","admin","admin");
 if (!$con)
   {
   die('Could not connect: ' . mysql_error());
   }
 mysql_select_db($db, $con);
    $arr = array();
   //Execute the query
        $rs = mysql_query("SELECT * FROM messages WHERE touser = '$enduser' GROUP BY fromuser");
        while($obj = mysql_fetch_object($rs)) {
        $arr[] = $obj;
         }
        echo '{"users":'.json_encode($arr).'}';
 mysql_close($con);
 ?>

即将到来的结果表明......

{"users":[{"id":"359","touser":"knitsu","fromuser":"skye","date":"2013-07-04 07:07:44","message ":"想你","照片":"","re​​admessage":"NO"},{"id":"327","touser":"knitsu","fromuser":"tota","date ":"2013-04-08 05:49:46","message":"不只是令人不寒而栗","photo":"","re​​admessage":"NO"}]}

这很好,除了一件事,它显示的消息是表中的第一个消息,这不是最新消息,我将如何对其进行排序以显示最新消息?

谢谢

4

1 回答 1

3

将您的查询更改为以下内容:

SELECT *
FROM messages a
LEFT JOIN messages b
    ON a.fromuser = b.fromuser
    AND a.date < b.date
WHERE b.date is null
AND a.touser = '$enduser' 
ORDER BY LOWER(a.fromuser)
于 2013-07-10T17:13:00.497 回答