PHP 如何返回每组用户的最新条目?

发布于 2024-12-03 16:09:40 字数 848 浏览 0 评论 0 原文

我正在尝试根据一组发件人的时间戳返回最新消息。

基本表和 qry 看起来像这样

SELECT senderName, messages
FROM MessageTable
WHERE receiverID = 1

(output)
Joe "Hey"
Bob "Yo"
Jim "Blah"
Joe "What's up"

我想从每个发件人那里获取最新消息。我想使用 ORDER BY 和 LIMIT 但它只从整个 qry 返回 1 条消息。这不是我正在寻找的结果。

SELECT senderName, messages
FROM MessageTable
WHERE receiverID = 1
ORDER BY sentDate ASC LIMIT 1

(output)
Bob "Yo"

我希望有这种输出,其中仅返回 Joe 的最新消息以及其他发件人的最新消息。

Bob "Yo"
Jim "Blah"
Joe "What's up"

我看过 stackoverflow 上的另一个问题,但这对我来说没有意义。 https: //stackoverflow.com/questions/384142/how-to-get-latest-record-for-each-day-when-there-are-multiple-entries-per-day

谢谢!

I am trying to return back the latest message based on timestamp from a group of Senders.

The basic table and qry looks like this

SELECT senderName, messages
FROM MessageTable
WHERE receiverID = 1

(output)
Joe "Hey"
Bob "Yo"
Jim "Blah"
Joe "What's up"

I want to get the latest message from EACH sender. I thought to use ORDER BY and LIMIT but it just returns 1 message from the entire qry. Which is not the result I’m looking for.

SELECT senderName, messages
FROM MessageTable
WHERE receiverID = 1
ORDER BY sentDate ASC LIMIT 1

(output)
Bob "Yo"

I would like to have this kind of output, where only Joe's latest message is returned along with the other Sender's latest message.

Bob "Yo"
Jim "Blah"
Joe "What's up"

I've looked at another question on stackoverflow, but it didn't make sense to me.
https://stackoverflow.com/questions/384142/how-to-get-latest-record-for-each-day-when-there-are-multiple-entries-per-day

Thank you!

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

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

发布评论

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

评论(3

星光不落少年眉 2024-12-10 16:09:40

GROUP BY 是你的朋友http://dev.mysql。 com/doc/refman/5.0/en/group-by-modifiers.html

你可以做这样的事情:

SELECT senderName, messages
FROM MessageTable
GROUP BY senderName
ORDER BY sentDate DESC

GROUP BY is your friend http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html

You could do some thing like this:

SELECT senderName, messages
FROM MessageTable
GROUP BY senderName
ORDER BY sentDate DESC
债姬 2024-12-10 16:09:40

好的,经过一些搜索和反复试验,这个查询返回了我正在寻找的内容:

SELECT a.senderName, a.messages, a.sentDate
FROM MessageTable AS a,
(
    SELECT senderName, MAX(sentDate) as max_date
    FROM MessageTable
    WHERE ReceiverID = 1
    GROUP BY senderName
) AS b
WHERE a.senderName = b.senderName
AND a.sentDate = b.max_date;

所有功劳都归于这个网站:http://forums.devarticles.com/general-sql-development-47/select-max-datetime-problem-10210.html

Ok, after some searching and trial and error, this query returned what i was looking for:

SELECT a.senderName, a.messages, a.sentDate
FROM MessageTable AS a,
(
    SELECT senderName, MAX(sentDate) as max_date
    FROM MessageTable
    WHERE ReceiverID = 1
    GROUP BY senderName
) AS b
WHERE a.senderName = b.senderName
AND a.sentDate = b.max_date;

All the credit goes to this site: http://forums.devarticles.com/general-sql-development-47/select-max-datetime-problem-10210.html

太傻旳人生 2024-12-10 16:09:40

查询的类型称为“排名查询”,它们通常需要一定程度的能力。 是一个类似的问题: 检索每个组中的最后一条记录

这 如果您使用数字 ID 而不是字符串来操作以使查询足够高效,那就更好了。

The type of the queries is called "Ranking queries" and they do usually require some level of competence. Here is a similar question: Retrieving the last record in each group

It is better if you would operate with numeric ids instead of strings to make the query efficient enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文