简体   繁体   English

Mysql:将最新评论排序为第一条评论,将最新评论排序为最后一条

[英]Mysql: Sorting newest comments to be the first and newest replies on each comment to be the last

I have a table with hierarchical-data (comments with replies) and I wonder how I can sort it so that the newest comments will be prepended as the first ones and the newest replies for each comment should be appended as the last ones . 我有一个带有层次结构数据的表(带有回复的评论),我想知道如何对其进行排序,以便将最新评论作为第一个 评论放在前面,并将每个评论的最新答复作为最后一个结尾添加

An example of the table 表的例子

Here is an example of what it looks like: 这是一个看起来像的例子:

id   |   path    |   reply_to   |   date
1         1            NULL         8:00
2        1-2            1           9:00
3        1-3            1          10:00
4         4            NULL        11:00
5        1-5            1          12:00
6        4-6            4          13:00
7        4-7            4          14:00

If I want to select the path of my result, it should be ordered like this: 如果要选择结果的路径,则应按以下顺序进行排序:

4
4-6
4-7
1
1-2
1-3
1-5

Is this possible using MySQL queries? 使用MySQL查询可以吗?

What I have been doing until now 直到现在我一直在做什么

Until now, I have only selected the rows with no replies ordered by ID DESC , and then (for each row) I requested its replies and ordered them by ID ASC , I used PDO in PHP to connect to my database: 到目前为止,我只选择了ID DESC排序没有答复的行,然后(每行)我请求了答复并按照ID ASC排序了,我在PHP中使用PDO连接到数据库:

$stmt1 = $this->db->query("SELECT * FROM comments WHERE reply_to IS NULL ORDER BY id DESC");
$stmt2 = $this->db->prepare("SELECT * FROM comments WHERE reply_to = ? ORDER BY id ASC");
while($row = $stmt1->fetch()) {
    //display the comment
    $stmt2->execute(array($row['id']));
    while($row2 = $stmt2->fetch()) {
        //display all its replies
    }
}

But I think if I query only one time it would be much better than querying inside of a loop like I am doing now, right? 但是我想,如果只查询一次,那会比像现在这样在循环内查询要好得多,对吗?

I hope my question is understandable. 我希望我的问题是可以理解的。 Unfortunately, I have no idea how to solve this problem. 不幸的是,我不知道如何解决这个问题。 I thought that SQL Joins are the solution, but as far as I know they are "used to combine rows from two or more tables, based on a common field between them". 我以为SQL连接是解决方案,但据我所知,它们“用于基于两个或多个表之间的公共字段来合并行”。

Here is an SQL Fiddle to play around. 这是一个SQL提琴演奏。

Thanks in advance. 提前致谢。

This is a bit complicated, but you can do it (at least for the data shown in the question). 这有点复杂,但是您可以做到(至少对于问题中显示的数据而言)。 The idea is to bring in the parent information ("reply to"). 这个想法是引入父信息(“答复”)。 Then sort by the following: 然后按以下顺序排序:

  • Parent's date and parent's id -- so all parent messages are together 父母的日期和父母的ID-因此所有父母消息都在一起
  • Then put the parent first 然后把父母放在第一位
  • Then order the rest by the time 然后按时间订购其余的

This is the query: 这是查询:

select c.*
from comments c left join
     comments cparent
     on c.reply_to = cparent.id
order by coalesce(cparent.date, c.date) desc,
         coalesce(cparent.id, c.id),
         (cparent.id is null) desc,
         c.date asc;

easiest way: 最简单的方法:

  SELECT c.* FROM 
    comments c
  ORDER BY LEFT(c.path,1) DESC, c.path ASC

You could probably do it in one query doing something like that 您可能可以在一个查询中执行类似的操作

SELECT comment.path
FROM comments AS base
LEFT JOIN comments AS comment ON (
    comment.id = base.id OR -- join the parents
    comment.reply_to = base.id -- join the children
)
WHERE base.reply_to IS NULL
ORDER BY comment.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM