我正在尝试对会话等消息进行分组,并仅显示会话中的最新消息,因此我需要该列中数字最高的消息time
。
这是我的查询:
SELECT *
FROM console_msgs
WHERE `to` = $user[id]
GROUP BY `from`
ORDER BY time DESC
谢谢你的帮助!
你想要分组最大值:
SELECT console_msgs.*
FROM console_msgs NATURAL JOIN (
SELECT MAX(time) AS time
FROM console_msgs
WHERE `to` = $user[id]
GROUP BY `from`
) t
WHERE `to` = $user[id]
我不太了解您需要返回的结果集,但我认为您需要使用 LIMIT 关键字来仅返回最高结果。试试这个:
SELECT from, MAX(time)
FROM console_msgs
WHERE `to` = $user[id]
GROUP BY `from`
ORDER BY time DESC
LIMIT 1
你不会想用它GROUP BY
来实现我认为你所要求的。你只想要多个订单
SELECT *
FROM console_msgs
WHERE `to` = $user[id]
ORDER BY `from`, time DESC