>我有一个包含这些列的消息表
- 编号
- item_id
- user_from
- user_to
- 消息
- date_created
任何两个用户之间的消息都是关于给定item_id的。我想为用户创建一个对话列表。那么我如何让 MYSQL 仅返回每个item_id的最新消息列表,其中用户可以user_from或user_to。
例如,"对于 user_from = 10 或 user_to = 10 的每个item_id返回最新消息"。
任何帮助将不胜感激。
据我了解,您正在寻找的查询将是这样的:
SELECT `message` FROM `messages`
WHERE `user_from` = "id"
OR `user_to` = "id"
GROUP BY `item_id`
ORDER BY `date_created` DESC
这意味着:
Get `message`
when `user_from` equals "id" or `user_to` equals "id"
but only once for every `item_id`
ordered by the latest date downwards
我希望这有所帮助。