php Mysql 对用户消息进行分组和排序
PHP Mysql 表:消息
id sender receiver time_sent Message SendDel RecDel
1 1 3 2011-08-17 14:00:00 [text] 0 0
2 3 1 2011-08-17 15:00:00 [text] 0 0
3 2 4 2011-08-18 14:19:28 [text] 1 0
4 4 2 2011-08-18 15:19:28 [text] 0 0
目标是检索最高值消息(MAX)并根据发送者、接收者对消息进行分组。因此,消息 id 的 1 和 2 将组合在一起,而 id 的 3 和 4 将组合在一起。
示例:登录 userid = 2 应该返回
id sender receiver time_sent Message SendDel RecDel
4 4 2 2011-08-18 15:19:28 [text] 0 0
我不确定为什么,但我的查询没有将所有消息分组在一起。这是我的疑问:
SELECT id, sender, receiver, MAX(time_sent), MAX(message)
FROM Messages
WHERE sender='$userid' OR receiver = '$userid'
Group By sender,receiver
Order BY time_sent DESC
有解决方案吗?
PHP Mysql Table:Messages
id sender receiver time_sent Message SendDel RecDel
1 1 3 2011-08-17 14:00:00 [text] 0 0
2 3 1 2011-08-17 15:00:00 [text] 0 0
3 2 4 2011-08-18 14:19:28 [text] 1 0
4 4 2 2011-08-18 15:19:28 [text] 0 0
Objective is to retrieve the highest value message(MAX) and group the messages based on sender,receiver. So message id's 1 and 2 would group together and id's 3 and 4 would group together.
Example: logged in userid = 2 Should return
id sender receiver time_sent Message SendDel RecDel
4 4 2 2011-08-18 15:19:28 [text] 0 0
Im not sure why but my query does not group all the messages together. Here is my query:
SELECT id, sender, receiver, MAX(time_sent), MAX(message)
FROM Messages
WHERE sender='$userid' OR receiver = '$userid'
Group By sender,receiver
Order BY time_sent DESC
Any Solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你想要:
您必须在 group by 子句中施展一些魔法才能实现这一点。
You want:
You'll have to do some magic in the group by clause to make this happen.