分组依据 排序依据 帮助

发布于 2024-11-02 08:27:37 字数 607 浏览 3 评论 0原文

以下代码有效,但我希望各组按日期时间显示最新查询,而不是第一个日期时间查询。我尝试更改 ASC/DESC 中的 ORDER BY,但这只是更改了所有组的顺序,而不是组内的数据。我需要组的所有内部数据以及所有按最新日期时间排序的组。

$query="SELECT * FROM emails 
        WHERE sentto='$sid' 
        GROUP BY sentto 
        ORDER BY datetime DESC LIMIT $eu, $limit ";

而不是显示组并按第一个查询对它们进行排序:
Sam Richards 2011 年 1 月 22 日发来的消息(这是一个群组)
John Smith 于2011 年 1 月 5 日发送的消息(这是一个群组)

我希望它显示群组并按最新查询对它们进行排序:
John Smith 2011 年 4 月 19 日发来的消息(这是一个群组)
Sam Richards 于2011 年 3 月 10 日发来的消息(这是一个群组)

请帮忙。

The following code works, but I want the groups to show their latest query by datetime not the first datetime query. I've tried changing around the ORDER BY from ASC/DESC, but that just changes the order of all the groups, not the data within the groups. I need for both all the inside data of the groups and all the groups to order by the latest datetime.

$query="SELECT * FROM emails 
        WHERE sentto='$sid' 
        GROUP BY sentto 
        ORDER BY datetime DESC LIMIT $eu, $limit ";

Instead of it showing groups and ordering them by the first query:
Message from Sam Richards on January 22, 2011 (this is a group)
Message from John Smith on January 5, 2011 (this is a group)

I want it to show groups and order them by the latest query:
Message from John Smith on April 19, 2011 (this is a group)
Message from Sam Richards on March 10, 2011 (this is a group)

Please help.

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

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

发布评论

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

评论(3

心舞飞扬 2024-11-09 08:27:38

我认为您的部分问题是您正在使用分组查询选择非聚合列。您应该明确希望它在聚合查询结果中返回哪些值。

SELECT sentto, MAX(datetime) AS datetime FROM emails
  GROUP BY sentto
  ORDER BY datetime desc LIMIT $eu, $limit;

我仍然不确定这是否能给你带来你想要的。听起来您想实际检索每封电子邮件的行,然后仅使用 GROUP BY 最大值进行排序。为此,您可能需要执行上述查询,然后返回并为每个 sendto 执行第二个查询。我想不出一种方法可以在单个查询中立即完成此操作。

SELECT * FROM emails
  WHERE sentto=$sid
  ORDER BY datetime DESC;

(对于第一个查询中返回的每个 sendto。)

I think part of your problem is that you are selecting non-aggregate columns with a group-by query. You should be explicit about which values you want it to return in the aggregate query result.

SELECT sentto, MAX(datetime) AS datetime FROM emails
  GROUP BY sentto
  ORDER BY datetime desc LIMIT $eu, $limit;

I'm still not sure that this gives you what you want. It sounds like you want to actually retrieve the rows for each individual email and just use the GROUP BY maximum for sorting. To do that, you'd probably need to do the above query, then go back and do a second query for each sentto. I can't think of a way offhand to do it in a single query.

SELECT * FROM emails
  WHERE sentto=$sid
  ORDER BY datetime DESC;

(For each sentto returned in the first query.)

ゝ杯具 2024-11-09 08:27:38

怎么样:

ORDER BY sentto ASC, datetime DESC

How about:

ORDER BY sentto ASC, datetime DESC
以歌曲疗慰 2024-11-09 08:27:38

要对组中的数据进行排序,您需要在 ORDER BY 子句中包含 sendto。

试试这个:

SELECT * FROM emails 
            WHERE sentto='$sid' 
            GROUP BY sentto 
            ORDER BY sentto, datetime DESC LIMIT $eu, $limit 

To sort the data with in the groups you need to include sentto in the ORDER BY clause.

Try this:

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