我想设置一个看起来像这样的多级评论:
1 this is the first comment (with 2 replies)
1.1 first reply (with 2 replies)
1.1.1 first reply of first
1.1.2 second reply of first
1.2 second reply (with 1 reply)
1.2.1 first reply of second
2 this is the second comment (with 0 reply)
3 this is the third comment (with 0 reply)
我刚刚发现了 drupal 方法,它对查询中的数据进行排序非常酷,但我不知道如何计算评论的回复数。我不知道是否可以在 1 个 sql 查询中做到这一点。
这是我的表结构:
CREATE TABLE IF NOT EXISTS `mb_post` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`topic_id` int(11) NOT NULL,
`user` int(11) NOT NULL,
`message` text COLLATE utf8_unicode_ci NOT NULL,
`date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`level` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`parent_post_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=0 ;
--
-- Dumping data for table `mb_post`
--
INSERT INTO `mb_post` (`id`, `topic_id`, `user`, `message`, `date`, `level`, `parent_post_id`) VALUES
(1, 99, 10, 'this is the first comment', '2013-10-18 11:00:00', '1', 0),
(2, 99, 20, 'this is the second comment', '2013-10-18 11:10:00', '2', 0),
(3, 99, 30, 'this is the third comment', '2013-10-18 11:20:00', '3', 0),
(4, 99, 20, 'first reply', '2013-10-18 14:30:00', '1.1', 1),
(5, 99, 50, 'first reply of first', '2013-10-18 15:00:00', '1.1.1', 4),
(6, 99, 70, 'second reply of first', '2013-10-18 15:30:00', '1.1.2', 4),
(7, 99, 80, 'second reply', '2013-10-18 14:45:00', '2.1', 2),
(8, 99, 90, 'first reply of second', '2013-10-18 15:15:00', '2.1.1', 7);
如您所见,我level
在 varchar 中有列(我找不到其他使用多小数的方法)。
有没有办法计算评论的回复数量?
非常感谢!
编辑:想要的结果:
id | topic_id | user | message | date | level | parent_post_id | count
1 | 99 | 10 | this is the first comment | 2013-10-18 11:00:00 | 1 | 0 | 2
4 | 99 | 20 | first reply | 2013-10-18 14:30:00 | 1.1 | 1 | 2
5 | 99 | 50 | first reply of first | 2013-10-18 15:00:00 | 1.1.1 | 4 | 0
6 | 99 | 70 | second reply of first | 2013-10-18 15:30:00 | 1.1.2 | 4 | 0
2 | 99 | 20 | this is the second comment| 2013-10-18 11:10:00 | 2 | 0 | 1
7 | 99 | 80 | second reply | 2013-10-18 14:45:00 | 2.1 | 2 | 1
8 | 99 | 90 | first reply of second | 2013-10-18 15:15:00 | 2.1.1 | 7 | 0
3 | 99 | 30 | this is the third comment | 2013-10-18 11:20:00 | 3 | 0 | 0
数据应按级别排序,计数列应为其直接子级数的结果。对于ie:评论1
应该计算所有评论的级别1.1, 1.2, 1.3
而不是1.1.1