SQL 组通过选择

发布于 2024-12-26 15:08:16 字数 637 浏览 2 评论 0原文

这是我的代码:

mysql_query("SELECT * FROM messages WHERE to_user_id = '$user_id' GROUP BY from_user_id ORDER BY read,sent_date DESC")

这是我的表结构

在此处输入图像描述

我使用 GROUPY BY from_user_id 语句来简要显示“对话”列表,而不是每条消息。像这样

在此处输入图像描述

但是,正如您在图像中看到的,前两个方向错误,第一个说“1周前”,下面的说“2天前”。这些顺序错误的原因是 GROUP BY from_user_id 语句造成的。因为它将来自该用户的所有消息分组,并且不会包含最近的时间。

所以我的问题是:

如何 GROUP BY from_user_id 按最新记录?

Here is my code:

mysql_query("SELECT * FROM messages WHERE to_user_id = '$user_id' GROUP BY from_user_id ORDER BY read,sent_date DESC")

and here is my table structure

enter image description here

I use the GROUPY BY from_user_id statement to briefly show a list of "conversations" instead of every single message. Like this

enter image description here

But, as you can see in the image, the top two are the wrong way round, the first one says "1 week ago" and the one below says "2 days ago". The reason for these being in the wrong order is due to the GROUP BY from_user_id statement. Because it groups all the messages from that user and it wont have the the most recent time on it.

So my question is:

How can I GROUP BY from_user_id by the most recent record?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清晨说晚安 2025-01-02 15:08:16

您不能SELECT未在GROUP BY中列出的列或允许的函数。这是GROUP BY 的工作原理

尽管大多数其他 SQL 风格在此类查询上都会失败,但 MySQL 不会。相反,它提供任意结果 - 您所看到的。

您可以通过几种不同的方式解决这个问题。例如:

  • 在单独的查询中使用 GROUP BYSELECT最新用户对话的ids
  • 通过子选择idsJOIN一组ids,在一个查询中完成所有操作。

You can not SELECT columns not listed in the GROUP BY or otherwise allowed functions. This is how GROUP BY works.

Although most other SQL flavors would fail on such a query, MySQL doesn't. Instead it provides arbitrary results - what you are seeing.

You can solve this a few different ways. For example:

  • SELECT the ids of the latest user conversations using GROUP BY in a separate query.
  • Do it all in one query by sub-selecting the ids or JOIN the set of ids.
梦明 2025-01-02 15:08:16

由于 MySQL 不支持像 ROW_NUMBER() OVER() 这样的窗口函数,您可以执行以下操作:

SELECT * 
FROM Messages 
where id in (SELECT MAX(ID) FROM messages GROUP BY from_user_id) 
ORDER BY sent_date, read 

子查询将只返回每个用户的最新消息 id。我假设您的 auto_increment 与消息发送的顺序相对应。即使这不是您可能想要的确切逻辑,这是一种从 MySQL 中有效的分组记录中获取特定值子集的通用技术。

Since MySQL doesn't support windowing functions like ROW_NUMBER() OVER() you can do something like this:

SELECT * 
FROM Messages 
where id in (SELECT MAX(ID) FROM messages GROUP BY from_user_id) 
ORDER BY sent_date, read 

The subquery will only return the newest message id for each user. I'm assuming your auto_increment corresponds with the order the messages are sent in. Even if it's not the exact logic you might want, this is a general technique to get a specific subset of values from grouped records that works in MySQL.

梦太阳 2025-01-02 15:08:16

尝试用这个

SELECT *,MAX(id) as OrderField FROM messages WHERE to_user_id = '$user_id' 
 GROUP BY from_user_id 
 ORDER BY OrderField DESC,read

Try with this

SELECT *,MAX(id) as OrderField FROM messages WHERE to_user_id = '$user_id' 
 GROUP BY from_user_id 
 ORDER BY OrderField DESC,read
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文