您好我正在尝试使用php和mysql创建一个消息系统。
mysql表很简单: ID 寄件人 接收器 文本 时间戳
我正在努力使消息传递有点像Facebook / Twitter,因此列表位于“对话”中,并且会在对话中最后一条消息。
这就是我的主题:
(SELECT * FROM messages WHERE receiver = 13 OR sender = 13 GROUP BY receiver,sender ORDER BY id ASC) ORDER BY id ASC
答案 0 :(得分:2)
SELECT messages.* FROM messages, (SELECT MAX(id) as lastid FROM messages
WHERE receiver = 13 OR sender = 13
GROUP BY CONCAT(LEAST(receiver,sender),'.',GREATEST(receiver,sender))) as conversations
WHERE id = conversations.lastid
ORDER BY timestamp DESC
您需要的是聊天伙伴之间的唯一对话ID。我用子查询模拟了这个,希望这有帮助
答案 1 :(得分:1)
<强>更新强>: 我不确定它是否完美:
SELECT * FROM messages
WHERE receiver = 13
GROUP BY receiver,sender
ORDER BY timestamp DESC
LIMIT 1
UNION ALL
SELECT * FROM messages
WHERE sender = 13
GROUP BY receiver,sender
ORDER BY timestamp DESC
LIMIT 1
撤消订单:
ORDER BY timestamp DESC
答案 2 :(得分:0)
接收新行的DESC。 AND ASC或NOT设置,默认为ASC:
(SELECT * FROM messages WHERE receiver = 13 OR sender = 13 GROUP BY receiver,sender ORDER BY id DESC)
AND SET LIMIT 1,1在订购后
我想你需要它
(receiver = 13 AND sender = 'usersender id' ) OR (receiver ='usersender id' AND sender = 13 )
试一试!