mysql - 使用闭包表对分层数据进行分页 (MySQL)

标签 mysql sql hierarchical-data transitive-closure-table

我正在尝试查询按分数 排序的分页分层评论。 score 是一个整数,comments 表有一个自引用的 parent_id 列。

每个页面都应该至少有一个根注释,后面跟着它的子注释。如果数据集中只有一个根评论,则只会返回一页。

因此,给定 comments 表中的以下数据:

+----+-------+-----------+
| id | score | parent_id |
+----+-------+-----------+
|  1 |    10 |      NULL |
|  2 |     5 |      NULL |
|  3 |     0 |         1 |
|  4 |     6 |         2 |
|  5 |     0 |      NULL |
|  6 |    30 |         1 |
|  7 |     1 |         3 |
|  8 |     0 |         4 |
|  9 |    50 |      NULL |
| 10 |     2 |         2 |
+----+-------+-----------+

我希望能够SELECT * FROM comments...LIMIT 4 OFFSET 0 并且Page 1是:

+----+-------+-----------+
| id | score | parent_id |
+----+-------+-----------+
|  9 |    50 |      NULL |
|  1 |    10 |      NULL |
|  6 |    30 |         1 |
|  3 |     0 |         1 |
+----+-------+-----------+

第 2 页是:

+----+-------+-----------+
| id | score | parent_id |
+----+-------+-----------+
|  2 |     5 |      NULL |
|  4 |     6 |         2 |
| 10 |     2 |         2 |
|  5 |     0 |      NULL |
+----+-------+-----------+

Page 3 为空白,因为没有根评论保留。

我正在使用支持闭包表,如 Bill Karwin 所述,因为可以使用任何评论作为根评论来独立查看评论子树,这似乎是最好的解决方案。

相关表的结构和样本数据如下:

CREATE TABLE `comments` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `score` int(11) NOT NULL,
  `parent_id` int(11) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `comments` (`id`, `score`, `parent_id`)
VALUES
  (1,10,NULL),
  (2,5,NULL),
  (3,0,1),
  (4,6,2),
  (5,0,NULL),
  (6,30,1),
  (7,1,3),
  (8,0,4),
  (9,50,NULL),
  (10,2,2);

CREATE TABLE `comments_closure` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `ancestor` int(11) unsigned NOT NULL,
  `descendant` int(11) unsigned NOT NULL,
  `depth` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `comments_closure` (`id`, `ancestor`, `descendant`, `depth`)
VALUES
  (1,1,0), (1,3,1), (1,6,1), (1,7,2),
  (2,2,0), (2,4,1), (2,10,1), (2,8,2),
  (3,3,0), (3,7,1),
  (4,4,0), (4,8,1),
  (5,5,0),
  (6,6,0),
  (7,7,0),
  (8,8,0),
  (9,9,0),
  (10,10,0);

最佳答案

这应该适用于 1 级深度查询:

SELECT @id_multiplier := MAX(POW(10, -(length(id) + 1))) FROM comments;
SELECT @score_multiplier := @id_multiplier * MAX(POW(10, -(length(score) + 1))) FROM comments;
SELECT c1.id
     , c1.score
     , c1.parent_id 
FROM comments c1
LEFT JOIN comments c2 
   ON c1.parent_id = c2.id
WHERE c1.parent_id IS NULL 
   OR c1.parent_id IN 
     (SELECT id FROM comments WHERE parent_id IS NULL)
ORDER BY 
   IF(ISNULL(c1.parent_id)
    , c1.score
    , c2.score + (c1.parent_id * @id_multiplier) - (1-(c1.score * @score_multiplier))
    ) DESC

关于mysql - 使用闭包表对分层数据进行分页 (MySQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16619682/

相关文章:

php mysql替代在循环内再次使用相同的查询

php - wordpress 上的 Mysql UNION

SQL Server 返回字段更改的行

r - 如何可视化分层数据?

sql - View 中可以有 OrderBy 吗?

sql - 使用sql中相互依赖的两列的先前记录的算术计算

php - 如何在 TreeView 中显示层次结构数据库?

php - 使用 php 将当前时间戳插入 mysql?

mysql - 在mysql中,如何在字段中具有0或1值的一组记录中选择值1的记录(仅当存在时)?

php - 将ajax数据发布到mysql中