按同一条目对评论进行分组,按日期排序
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用这个: Grouping arrays in PHP
我想我不应该写那么多有关我的 MySQL 表结构的详细信息,是使用 PHP 和数组完成的。感谢大家的关注。
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.
我想您会执行这样的查询
,并在结果集上循环,而entry_id 仍然相同。尽管根据您的问题我不确定您想要输出的位置。在代码中或在 sql 编辑器中。
I would imagine you do a query such as this one
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.
首先按entry_ID 分组,然后按最大日期分组。您关心的是最新的。 (至少 Facebook 是这样做的。)
这是 TSQL 中的代码
。在内部查询中,我选择了entry_id 和最新评论的日期。
然后我链接到该列表,按最新评论或最新条目(取决于最近发生的事情)排序,然后按entry_id排序(因此所有条目评论将首先按日期排列,最后按日期排序,因此条目将按正确的顺序
注意:我没有测试此代码,因此它可能有一些拼写错误。
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.