我有三个表:对话、消息和用户。表的解释是here。对话表:iduser1user2消息表:idconversation_idfromUsertoUseris_read用户表:idnameetc现在,我想获取特定用户 ID 的对话,假设为 210. 并且带有未读消息的对话应该放在最前面。任何帮助将不胜感激。编辑:我现在触发的查询是:SELECT * FROM `messages`join conversations on messages.conversation_id = conversations.idwhere messages.fromUser = 210 or messages.toUser = 210GROUP by messages.conversation_idORDER by messages.is_read ASC, messages.id DESC我得到的结果:https : //i.stack.imgur.com/NEeMs.png基于预期结果的对话 ID:281724and then others.
2 回答
动漫人物
TA贡献1815条经验 获得超10个赞
如果有人需要这个,我有解决方案。
SELECT *
FROM conversations
JOIN
(SELECT conversation_id, is_read, fromUser, toUser, MAX(id) as id FROM messages GROUP BY messages.id) message
ON message.conversation_id = conversations.id
WHERE conversations.user1 = 210 or conversations.user2 = 210
GROUP by conversations.id
ORDER by is_read = 0 desc, toUser !=210
- 2 回答
- 0 关注
- 126 浏览
添加回答
举报
0/150
提交
取消