我正在使用一个 mysql 查询,该查询应该选择用户寻址或发送的所有消息。我需要对具有相同 UID 的所有消息进行分组,以便为每个不同的用户显示一个线程(这意味着它应该消除除最后一个具有相同 UID 的消息之外的所有消息)。我的问题是我开始使用 GROUP BY 来执行此操作,但有时剩余的行实际上是旧消息而不是最新消息。
这就是我正在尝试的:
SELECT `UID`, `Name`, `Text`, `A`.`Date`
FROM `Users`
INNER JOIN (
(
SELECT *, To_UID AS UID FROM `Messages` WHERE `From_UID` = '$userID' AND `To_UID` != '$userID'
)
UNION ALL
(
SELECT *, From_UID AS UID FROM `Messages` WHERE `To_UID` = '$userID' AND `From_UID` != '$userID'
)
) AS A
ON A.UID = Users.ID
GROUP BY UID // This doesn't work
如何仅显示每个 UID 重新发送日期最多的行?