嗨我試圖使用php和mysql製作消息系統。使用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
嗨我試圖使用php和mysql製作消息系統。使用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
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。我用子查詢模擬了這個,希望這有助於
謝謝!這不是我的問題,但我一直在尋找同樣的東西,而且這個工作非常完美! – ShoeLace1291
更新: 我不知道,如果它完美的作品:
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
問題是我正在查看多個對話。我需要僅查看每個人1並查看通話中發送的最後一條消息。 – Josh
您可以在使用ORDER – user3106988
之後使用LIMIT 1,但我不會看到與其他人的對話。 – Josh
DESC用於接收新行。與ASC或沒有設置,默認爲ASC:
(SELECT * FROM messages WHERE receiver = 13 OR sender = 13 GROUP BY receiver,sender ORDER BY id DESC)
和SET LIMIT 1,1 ORDER後
我認爲你需要它
(receiver = 13 AND sender = 'usersender id') OR (receiver ='usersender id' AND sender = 13)
試試吧!
你可以按關鍵字分組後按desc嘗試。 – jeeva