0

我有一张submissions_comments有父母和孩子的桌子,每个孩子都有 1:1 的关系,每个 parnet 有 1:many 的关系。

也就是说,所有父母都可以有无限(1 级深的孩子),所有孩子只能有 1 个父母。

我当前的查询工作正常,因为它返回特定提交的所有评论,然后我可以在我的应用程序的前端进行嵌套,但是,当我为 ajax 分页引入偏移量时,我只抓住了父母在偏移量中,并且如果它们包含未在该特定偏移量中返回的子注释,则我的嵌套将不起作用。

如何修改我的查询以使用偏移量(例如 0,15 15,30 等)获取所有相关子评论

    SELECT parent.id,
      parent.deleted,
      parent.comment,
      parent.user_id,
      parent.created,
      parent.parent_id,
      s.user_id submissionUserId,
      u.username,
      u.photo AS userPhoto
    FROM submissions_comments AS parent
    LEFT JOIN users u ON parent.user_id = u.id
    LEFT JOIN submissions_comments AS child ON child.parent_id = parent.id
    LEFT JOIN submissions s ON parent.submission_id = s.id
    WHERE parent.submission_id = ?
    AND parent.deleted = 0
    GROUP BY parent.id
    ORDER BY parent.created DESC,
    child.created
    LIMIT 0,15 // or offset that I pass in from my front-end application.

这是一个带有数据/表的sqlfiddle:

http://sqlfiddle.com/#!2/924c8

这里还有一个乱序数据的例子(例如id= 5 的注释应该在第二组结果中(而不是第一组)

使用最初 0,15 的偏移量(因为这是页面上显示的默认评论数):

+-----+---------+-----------------------+---------+---------------------+-----------+------------------+----------+--------------------------------------+
| id  | deleted | comment               | user_id | created             | parent_id | submissionUserId | username | userPhoto                            |
+-----+---------+-----------------------+---------+---------------------+-----------+------------------+----------+--------------------------------------+
| 21 |       0 | Replying to comment 5 |      2 | 2014-08-08 01:14:42 |       5 |               58 | bobcobb2     | 772187eabd2df8ce62a2f1d59f61eb21.jpg |
| 20 |       0 | .... comment 20       |      1 | 2014-08-08 01:13:31 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 19 |       0 | .... comment 19       |      1 | 2014-08-08 01:13:29 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 18 |       0 | .... comment 18       |      1 | 2014-08-08 01:13:26 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 17 |       0 | .... comment 17       |      1 | 2014-08-08 01:13:23 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 16 |       0 | .... comment 16       |      1 | 2014-08-08 01:13:18 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 15 |       0 | .... comment 15       |      1 | 2014-08-08 01:13:16 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 14 |       0 | .... comment 14       |      1 | 2014-08-08 01:13:13 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 13 |       0 | .... comment 13       |      1 | 2014-08-08 01:13:11 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 12 |       0 | .... comment 12       |      1 | 2014-08-08 01:13:06 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 11 |       0 | .... comment 11       |      1 | 2014-08-08 01:13:05 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 10 |       0 | .... comment 10       |      1 | 2014-08-08 01:13:04 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 9 |       0 | .... comment 9        |      1 | 2014-08-08 01:13:02 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 8 |       0 | .... comment 8        |      1 | 2014-08-08 01:13:00 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 7 |       0 | .... comment 7        |      1 | 2014-08-08 01:12:57 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
+-----+---------+-----------------------+---------+---------------------+-----------+------------------+----------+--------------------------------------+

然后在您单击页面上的“加载更多评论...”链接以获取(较旧的)评论时传递偏移量 15,30。

+-----+---------+----------------+---------+---------------------+-----------+------------------+----------+--------------------------------------+
| id  | deleted | comment        | user_id | created             | parent_id | submissionUserId | username | userPhoto                            |
+-----+---------+----------------+---------+---------------------+-----------+------------------+----------+--------------------------------------+
| 6 |       0 | .... comment 6 |      1 | 2014-08-08 01:12:54 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 5 |       0 | .... comment 5 |      1 | 2014-08-08 01:12:51 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 4 |       0 | .... comment 4 |      1 | 2014-08-08 01:12:50 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 3 |       0 | .... comment 3 |      1 | 2014-08-08 01:12:48 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 2 |       0 | .... comment 2 |      1 | 2014-08-08 01:12:47 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
| 1 |       0 | .... comment 1 |      1 | 2014-08-08 01:12:41 |      NULL |               58 | bobcobb    | 935dc24b4529374bc1efcb782411f50e.png |
+-----+---------+----------------+---------+---------------------+-----------+------------------+----------+--------------------------------------+
4

1 回答 1

0

我对这个问题的理解是,其中一条评论被乱序返回。目前,您的查询正在按创建日期降序排列所有评论。这通常是正确的,除非一条评论是对另一条评论的回复。然后,“子”评论需要与父评论同时返回。这是一个将实现此排序的查询。

SELECT parent.id,
  parent.deleted,
  parent.comment,
  parent.user_id,
  parent.created,
  parent.parent_id,
  s.id as sid,
  s.user_id submissionUserId,
  u.username,
  u.photo AS userPhoto
FROM submissions_comments AS parent
LEFT JOIN users u ON parent.user_id = u.id
LEFT JOIN submissions s ON parent.submission_id = s.id
WHERE parent.submission_id = ?
AND parent.deleted = 0
GROUP BY parent.id
ORDER BY Case when parent.parent_id is null Then
          parent.created
         Else (Select p.created
               From submissions_comments p
               Where p.id = parent.parent_id) End DESC,
parent.id
LIMIT 15,30

对原始查询的更改如下:

  • 1) 删除了submissions_comments 表的左连接。我删除了这个联接,因为该表仅在 order by 中使用。我在 order by 子句中使用了同一个表,但自联接越少越好。
  • 2) 现在的订单包括一个案例陈述。

order by 需要有一个 case 语句,因为如果记录有 parent_id,我们需要完全改变结果的排序方式。如果 parent_id 为空,case 语句将返回创建日期。如果 parent_id 不为 null,则 case 语句将返回父级的创建日期。这将导致子评论在父评论旁边排序。

然后,我按 id 排序,以便父 id 排在第一位。这可能不是您想要的,您可以按 parent.created desc 排序,以便在父评论之前返回所有子评论。这是它的样子,使用您提供的 sql fiddle。

示例结果(并非包括所有列):

|id|deleted|user_id| created             | parent_id |submissionUserId| username |
+-----+---------+----------------+---------+---------------------+-----------+------------------+----------+--------------------------------------+
| 6|     0 |     1 | 2014-08-08 01:12:54 |      NULL |             58 | bobcobb |
| 5|     0 |     1 | 2014-08-08 01:12:51 |      NULL |             58 | bobcobb |
|21|     0 |     2 | 2014-08-08 01:14:42 |         5 |             58 | bobcobb2|
| 4|     0 |     1 | 2014-08-08 01:12:50 |      NULL |             58 | bobcobb |
| 3|     0 |     1 | 2014-08-08 01:12:48 |      NULL |             58 | bobcobb |

此查询的唯一问题是限制将取代其他所有内容。如果父项列在第 15 行,而子项列在第 16 行,则这些结果将不会一起返回。限制将在第 15 行停止。

我想提到的唯一另一件事是mysql不允许您将参数传递给LIMIT子句。如果您计划传递应作为参数返回的范围,您可能需要重新考虑查询的这一部分。一种可能的解决方案是创建一个临时表并分配临时 ID(基于结果的顺序),然​​后仅返回临时 ID 在您正在查找的范围之间的结果。

于 2014-08-11T21:24:03.810 回答