按同一条目对评论进行分组,按日期排序

发布于 2024-12-02 20:34:16 字数 944 浏览 1 评论 0原文

我有一个名为 comments 的表,其结构如下:

id | entry_id | date | comment

我还有一个名为 entries 的表,其结构如下:

entry_id | title | date | entry

我只想显示最近的评论和条目,其中添加了这些评论。

现在我正在使用这个查询:

SELECT c.id,
       c.date,
       c.comment,
       e.entry_id,
       e.title
FROM   entries e
       INNER JOIN comments c
        ON e.entry_id = c.entry_id
GROUP  BY c.date DESC
LIMIT  50

我输出结果如下:

#entry_id
  1 hour ago:
  Some comment

#entry_id
  2 hours ago:
  Some comment    

评论按日期排序。我想做的只是按相同的 entry_id 对评论进行分组,例如:

#entry_id
  1 hour ago:
  Some comment

  2 hours ago:
  Some comment without  repeating the `entry_id`

#other entry_id
  5 hours ago:
  Some comment

我该怎么做?不需要为我编写代码,只需说出会如何做到这一点(例如,用伪代码)。这是像 Facebook 或 google+ 流上的评论分组,我希望你明白我的意思。

I have table called comments with following structure:

id | entry_id | date | comment

I also have a table called entries with following structure:

entry_id | title | date | entry

I just want to show recent comments and entries, to which these comments were added.

Now I'm using this query:

SELECT c.id,
       c.date,
       c.comment,
       e.entry_id,
       e.title
FROM   entries e
       INNER JOIN comments c
        ON e.entry_id = c.entry_id
GROUP  BY c.date DESC
LIMIT  50

And I output the results so:

#entry_id
  1 hour ago:
  Some comment

#entry_id
  2 hours ago:
  Some comment    

Comments are ordered by date. What I'm trying to do is simply group comments by same entry_id, e.g.:

#entry_id
  1 hour ago:
  Some comment

  2 hours ago:
  Some comment without  repeating the `entry_id`

#other entry_id
  5 hours ago:
  Some comment

How would I do that? No need to write a code for me, just say how would you do that (in pseudo code, for example). This is comments grouping like on facebook or google+ stream, I hope you understand what I mean.

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

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

发布评论

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

评论(3

倥絔 2024-12-09 20:34:16

使用这个: Grouping arrays in PHP

我想我不应该写那么多有关我的 MySQL 表结构的详细信息,是使用 PHP 和数组完成的。感谢大家的关注。

$groups = array();

foreach($items as $item)
    $groups[$item['value']][] = $item;

Used this: Grouping arrays in PHP

I think I shouldn't write so much details about my MySQL tables structure, done it with PHP and arrays. Thanks to all for attention.

$groups = array();

foreach($items as $item)
    $groups[$item['value']][] = $item;
眼泪都笑了 2024-12-09 20:34:16

我想您会执行这样的查询

select
  e.entry, e.title, c.comment, c.date
from 
  comments c join entries e on c.entry_id = e.entry_id
order by 
  e.entry_id, c.date desc

,并在结果集上循环,而entry_id 仍然相同。尽管根据您的问题我不确定您想要输出的位置。在代码中或在 sql 编辑器中。

I would imagine you do a query such as this one

select
  e.entry, e.title, c.comment, c.date
from 
  comments c join entries e on c.entry_id = e.entry_id
order by 
  e.entry_id, c.date desc

and the loop over your result set while entry_id is still the same. Although I'm not sure based on your question where you want the output. In the code or in the sql editor.

Spring初心 2024-12-09 20:34:16

首先按entry_ID 分组,然后按最大日期分组。您关心的是最新的。 (至少 Facebook 是这样做的。)

这是 TSQL 中的代码

。在内部查询中,我选择了entry_id 和最新评论的日期。

然后我链接到该列表,按最新评论或最新条目(取决于最近发生的事情)排序,然后按entry_id排序(因此所有条目评论将首先按日期排列,最后按日期排序,因此条目将按正确的顺序

注意:我没有测试此代码,因此它可能有一些拼写错误。

SELECT * FROM
  (SELECT e.entry_id, MAX(c.date) as latest
  FROM   entries e
  INNER JOIN comments c ON e.entry_id = c.entry_id
  GROUP BY e.entry_id DESC, max(c.date)
  ) AS LIST
LEFT JOIN entries e ON e.entry_id = LIST.entry_ID
LEFT JOIN comments c on c.entry_ID = LIST.entry_ID
ORDER BY MAX(e.date,list.latest) as time_of_entry_or_comment DESC,
                                    e.entry_ID, 
                                    c.date DESC

First group by the entry_ID and then group by the max date. What you care about is the most recent one. (This is how Facebook does it at least.)

Here is the code in TSQL

In the inner query I select the entry_id and the latest comment's date.

Then I link to that list ordering by either the latest comment or the latest entry (depending on what happened most recently) and then ordering them by entry_id (so all of an entries comments will be first and finally by the date so the comments within an entry will be in the correct order.

Note: I did not test this code so it might have some typos.

SELECT * FROM
  (SELECT e.entry_id, MAX(c.date) as latest
  FROM   entries e
  INNER JOIN comments c ON e.entry_id = c.entry_id
  GROUP BY e.entry_id DESC, max(c.date)
  ) AS LIST
LEFT JOIN entries e ON e.entry_id = LIST.entry_ID
LEFT JOIN comments c on c.entry_ID = LIST.entry_ID
ORDER BY MAX(e.date,list.latest) as time_of_entry_or_comment DESC,
                                    e.entry_ID, 
                                    c.date DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文