1

我有一个 PHP 消息传递系统,其工作方式如下。

message_thread 表 id(pk)、user1_deleted、user2_deleted

消息 id,thread_id(fk),消息,message_date,sender_id,receiver_id,sender_deleted,receiver_deleted

我试图通过使用以下命令查询消息表来获取包含最后一条消息的线程列表

$sql = "SELECT thread_id, msg, sender_id, receiver_id, MAX(message_date) AS msg_date FROM messages WHERE (sender_id = '$user_id' AND sender_deleted = 0) OR (receiver_id = '$user_id' AND receiver_deleted = 0) GROUP BY thread_id.

这有效,但不显示最后一条消息。它只显示第一条消息。

有谁知道可能出了什么问题?

谢谢

4

2 回答 2

2

这对我有用

$sql = "SELECT * FROM ( 
    SELECT id, thread_id, msg, sender_id, receiver_id, msg_date 
    FROM messages
    WHERE (sender_id = '$user_id' AND sender_deleted = 0) OR (receiver_id = '$user_id' AND receiver_deleted = 0)
    ORDER BY msg_date DESC
) AS SQLi GROUP BY thread_id";

感谢所有的贡献。

于 2013-07-26T21:28:57.743 回答
1

以此结束您的查询以反转顺序

 ORDER BY message_date DESC

至于为什么只有一个结果..我们要看看实际的脚本是什么样子的。

于 2013-07-26T18:48:55.987 回答