Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL按单个结果分组按时间排序_Mysql - Fatal编程技术网

MySQL按单个结果分组按时间排序

MySQL按单个结果分组按时间排序,mysql,Mysql,我正在尝试从messages表中获取用户,该表已向我发送消息,但每个发送者只需要一个结果,即创建列的最大值,即消息创建时间 SELECT messages.from, account_images.profile, bio.user_full_name FROM messages INNER JOIN account_images ON account_images.uuid=messages.from INNER JOIN bio ON bio.uuid=messages.from WHERE

我正在尝试从messages表中获取用户,该表已向我发送消息,但每个发送者只需要一个结果,即
创建
列的最大值,即消息创建时间

SELECT messages.from, account_images.profile, bio.user_full_name
FROM messages
INNER JOIN account_images ON account_images.uuid=messages.from
INNER JOIN bio ON bio.uuid=messages.from
WHERE messages.to='me'
GROUP BY messages.from
ORDER BY messages.creation DESC
最近创建其消息的用户必须位于顶部,但使用此代码不能位于顶部。我提到了,但什么也没有得到
有什么帮助吗

我发现这个mysql文档在这样的情况下非常有用:


您可以将当前查询编写为

SELECT DISTINCT messages.from, account_images.profile, bio.user_full_name
FROM messages
INNER JOIN account_images ON account_images.uuid=messages.from
INNER JOIN bio ON bio.uuid=messages.from
WHERE messages.to='me'
这可能会稍微更有效一些——但正如您在当前查询中注意到的,它不会按最新消息排序

这将根据您的要求进行排序:

SELECT messages.from, account_images.profile, bio.user_full_name
FROM messages
INNER JOIN account_images ON account_images.uuid=messages.from
INNER JOIN bio ON bio.uuid=messages.from
WHERE messages.to='me'
GROUP BY messages.from
ORDER BY MAX(messages.creation) DESC;

使用DISTINCT而不是聚合可以得到您想要的结果,但它既不高效也不优雅。

您的表是如何构造的?MySQL邪恶的
分组行为的另一个受害者。表结构:
id | | from | | message | creation
SELECT messages.from, account_images.profile, bio.user_full_name
FROM messages
INNER JOIN account_images ON account_images.uuid=messages.from
INNER JOIN bio ON bio.uuid=messages.from
WHERE messages.to='me'
GROUP BY messages.from
ORDER BY MAX(messages.creation) DESC;