我有一张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 |
+-----+---------+----------------+---------+---------------------+-----------+------------------+----------+--------------------------------------+