分组依据 排序依据 帮助
以下代码有效,但我希望各组按日期时间显示最新查询,而不是第一个日期时间查询。我尝试更改 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您的部分问题是您正在使用分组查询选择非聚合列。您应该明确希望它在聚合查询结果中返回哪些值。
我仍然不确定这是否能给你带来你想要的。听起来您想实际检索每封电子邮件的行,然后仅使用 GROUP BY 最大值进行排序。为此,您可能需要执行上述查询,然后返回并为每个 sendto 执行第二个查询。我想不出一种方法可以在单个查询中立即完成此操作。
(对于第一个查询中返回的每个 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.
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.
(For each sentto returned in the first query.)
怎么样:
How about:
要对组中的数据进行排序,您需要在
ORDER BY
子句中包含 sendto。试试这个:
To sort the data with in the groups you need to include sentto in the
ORDER BY
clause.Try this: