获取对话和消息。最后一条未读消息的对话应该在最上面

我有三个表:对话、消息和用户。


表的解释是here。对话表:


id

user1

user2

消息表:


id

conversation_id

fromUser

toUser

is_read

用户表:


id

name

etc

现在,我想获取特定用户 ID 的对话,假设为 210. 并且带有未读消息的对话应该放在最前面。


任何帮助将不胜感激。


编辑:我现在触发的查询是:


SELECT * FROM `messages`

join conversations on messages.conversation_id = conversations.id

where messages.fromUser = 210 or messages.toUser = 210

GROUP by messages.conversation_id

ORDER by messages.is_read ASC, messages.id DESC

我得到的结果:https : //i.stack.imgur.com/NEeMs.png


基于预期结果的对话 ID:


2

8

1724

and then others.


弑天下
浏览 130回答 2
2回答

动漫人物

如果有人需要这个,我有解决方案。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.idWHERE conversations.user1 = 210 or conversations.user2 = 210GROUP by conversations.idORDER by is_read = 0 desc, toUser !=210

白板的微信

应该像添加ORDER BY is_read, id DESC到数据库查询一样简单。我们无法提供更多帮助,因为您没有提供任何可供我们使用的东西。
打开App,查看更多内容
随时随地看视频慕课网APP