1

我正在尝试对会话等消息进行分组,并仅显示会话中的最新消息,因此我需要该列中数字最高的消息time

这是我的查询:

SELECT * 
FROM console_msgs 
WHERE `to` = $user[id] 
GROUP BY `from` 
ORDER BY time DESC

谢谢你的帮助!

4

3 回答 3

1

你想要分组最大值

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]
于 2012-08-31T15:32:53.393 回答
0

我不太了解您需要返回的结果集,但我认为您需要使用 LIMIT 关键字来仅返回最高结果。试试这个:

SELECT from, MAX(time) 
FROM console_msgs 
WHERE `to` = $user[id] 
GROUP BY `from` 
ORDER BY time DESC
LIMIT 1
于 2012-08-31T15:32:22.043 回答
0

你不会想用它GROUP BY来实现我认为你所要求的。你只想要多个订单

SELECT * 
FROM console_msgs 
WHERE `to` = $user[id] 
ORDER BY `from`, time DESC
于 2012-08-31T15:36:45.093 回答