本文介绍了mysql:按用户获取上次对话记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要通过 to_user 按降序获取最后的对话记录.我有一个叫做消息的表.请查看以下屏幕截图:
I need to get last conversation records by to_user in Descending order.I have table called messages. Please have a look on the following screen shot:
我希望以下列方式输出:
I want the output in the following manner:
from_user| to_user | message |
241 | 226 | How are you? |
241 | 256 | Hi test message |
我试过这个查询:
SELECT * FROM `messages` where from_user=241 group by to_user order by created DESC
我得到以下错误的输出:
提前致谢.
推荐答案
试试他的:
SELECT t1.*
FROM `messages` AS t1
JOIN (
SELECT to_user, MAX(created) AS created
FROM `messages`
WHERE from_user=241
GROUP BY to_user
) AS t2 ON t1.to_user = t2.to_user AND t1.created = t2.created
WHERE from_user=241
ORDER BY t1.created DESC
这篇关于mysql:按用户获取上次对话记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!