Question

I would like to set up a multi level comments that will look like that :

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)

I just discovered the drupal method which is pretty cool to sort the datas in the query but i don't know how to count the number of replies a comment has. I don't know if it's possible to do that in 1 sql query.

Here's my table structure :

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);

As you can see i have the level column in varchar (i couldn't find other way to use multi decimal).

Is there a way to count the number of reply a comment has ?

Thank you very much!

EDIT : Wanted result :

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

The datas should be sorted by level and the count column should be the result of the number of its direct child. For ie: the comment 1 should count all the comments with level 1.1, 1.2, 1.3 but not 1.1.1

Was it helpful?

Solution

SELECT p.id, COUNT(c.id), GROUP_CONCAT(c.id) 
FROM mb_post p LEFT OUTER JOIN mb_post c ON c.level LIKE CONCAT(p.level, '.%') 
GROUP BY p.id;

+----+-------------+--------------------+
| id | COUNT(c.id) | GROUP_CONCAT(c.id) |
+----+-------------+--------------------+
|  1 |           3 | 4,5,6              |
|  2 |           2 | 7,8                |
|  3 |           2 | 7,8                |
|  4 |           2 | 5,6                |
|  5 |           0 | NULL               |
|  6 |           0 | NULL               |
|  7 |           1 | 8                  |
|  8 |           0 | NULL               |
+----+-------------+--------------------+

If you want the count of replies for one specific comment, just use WHERE p.id = ? before the GROUP BY.


Count only immediate children, not all descendants:

SELECT p.id, COUNT(c.id), GROUP_CONCAT(c.id)
FROM mb_post p LEFT OUTER JOIN mb_post c 
  ON c.level RLIKE CONCAT('^', p.level, '[.][[:digit:]]+$')
GROUP BY p.id;

+----+-------------+--------------------+
| id | COUNT(c.id) | GROUP_CONCAT(c.id) |
+----+-------------+--------------------+
|  1 |           1 | 4                  |
|  2 |           1 | 7                  |
|  3 |           1 | 7                  |
|  4 |           2 | 5,6                |
|  5 |           0 | NULL               |
|  6 |           0 | NULL               |
|  7 |           1 | 8                  |
|  8 |           0 | NULL               |
+----+-------------+--------------------+

Re your comment:

You could use parent_post_id, except as I test it, your parent_post_id's aren't consistent with the hierarchy shown in the level column. For example, #7 is a child of #2 in level, but it's the child of #1 in the parent_post_id:

SELECT p.id, COUNT(c.id), GROUP_CONCAT(c.id)
FROM mb_post p LEFT OUTER JOIN mb_post c ON c.parent_post_id = p.id
GROUP BY p.id;

+----+-------------+--------------------+
| id | COUNT(c.id) | GROUP_CONCAT(c.id) |
+----+-------------+--------------------+
|  1 |           2 | 7,4                |
|  2 |           0 | NULL               |
|  3 |           0 | NULL               |
|  4 |           2 | 5,6                |
|  5 |           0 | NULL               |
|  6 |           0 | NULL               |
|  7 |           1 | 8                  |
|  8 |           0 | NULL               |
+----+-------------+--------------------+

This is the risk of storing information redundantly -- it can get out of sync.

OTHER TIPS

If I understand correctly, you want the first table as output. This is not really a table, because the columns do not line up. That means, string concatenation.

The keys are then to get the indents done properly and to count the number of replies, as in:

select concat(space(char_length(level)),
              level, ' ', message,
              coalesce(concat(' (with ', r.NumReplies, ' replies)'), ''))
from mb_post p left outer join
     (select parent_post_id, count(*) as NumReplies
      from mb_post
      group by parent_post_id
     ) r
     on p.id = r.parent_post_id
order by level;

This SQLFiddle gives a good idea of the result. However, the initial indentation doesn't work (despite the space()), which I think is a "feature" of SQLFiddle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top