数据库
首页 > 数据库> > 带有PHP / MySQL的消息系统

带有PHP / MySQL的消息系统

作者:互联网

嗨,我正在尝试使用php和mysql创建消息系统.

mysql表很简单:
ID
寄件人
接收器
文本
时间戳

我正在尝试使消息传递有点像Facebook / Twitter,以便该列表位于“对话”中,并查看对话中的最后一条消息.

这是我的atm:

(SELECT * FROM messages WHERE receiver = 13 OR sender = 13 GROUP BY receiver,sender ORDER BY id ASC) ORDER BY id ASC

解决方法:

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.我已经用子查询模拟了这一点,希望对您有所帮助

标签:php,mysql,messaging
来源: https://codeday.me/bug/20191011/1896032.html