如何从MySQL表中获取所有注释

时间:2013-12-27 06:26:06

标签: mysql count comments

好吧,我想计算并显示我的表(故事)的评论。它的结构是这样的:

id | type    | post        | parent

1  | story   | bla bla bla | 0  

2  | comment | text...     | 1  

3  | comment | text...     | 1 

4  | comment | text...     | 3 

5  | comment | other...    | 1 

如您所见,所有信息都放在一张表中。

例如,在这种情况下,我需要显示“故事”并显示它有多少评论。

我必须列出它的层次。

这样的事情:

ID. 1 - bla bla bla

Comments

|--ID. 2 text...

|--ID. 3 text...

...|--ID. 4. text.. (comment of ID 3)

|--ID. 5 other..

我知道如何列出故事及其ID与故事相同时的所有评论。但是我不知道我需要做些什么来展示和计算其他没有主要故事延伸的东西。

最好的方法是什么?

提前多多感谢。

1 个答案:

答案 0 :(得分:0)

试试这个 sqlFiddle

SELECT root.id as rootid,
       root.type as roottype,
       root.post as rootpost,
       child.id as childid,
       child.type as childtype,
       child.post as childpost,
       NULL as grandchildid,
       NULL as grandchildtype,
       NULL as grandchildpost
FROM stories as root
INNER JOIN stories as child ON child.parent = root.id
WHERE root.parent = 0
  AND root.id = 1
UNION
SELECT root.id as rootid,
       root.type as roottype,
       root.post as rootpost,
       child.id as childid,
       child.type as childtype,
       child.post as childpost,
       grandchild.id as grandchildid,
       grandchild.type as grandchildtype,
       grandchild.post as grandchildpost
FROM stories as root
INNER JOIN stories as child ON child.parent = root.id
INNER JOIN stories as grandchild ON grandchild.parent = child.id
WHERE root.parent = 0
  AND root.id = 1
ORDER BY childid,grandchildid

那么如果你想要一些看起来像你在上面提到的问题的输出你可以做一些像这样的事情(sqlFiddle

SELECT IF(grandchildid IS NULL,
      CONCAT("--ID. ",childid," ",childpost),
      CONCAT("...--ID. ",grandchildid," ",grandchildpost,"(comment of ID ",childid,")")
      ) as result
FROM
(
SELECT root.id as rootid,
       root.type as roottype,
       root.post as rootpost,
       child.id as childid,
       child.type as childtype,
       child.post as childpost,
       NULL as grandchildid,
       NULL as grandchildtype,
       NULL as grandchildpost
FROM stories as root
INNER JOIN stories as child ON child.parent = root.id
WHERE root.parent = 0
  AND root.id = 1
UNION
SELECT root.id as rootid,
       root.type as roottype,
       root.post as rootpost,
       child.id as childid,
       child.type as childtype,
       child.post as childpost,
       grandchild.id as grandchildid,
       grandchild.type as grandchildtype,
       grandchild.post as grandchildpost
FROM stories as root
INNER JOIN stories as child ON child.parent = root.id
INNER JOIN stories as grandchild ON grandchild.parent = child.id
WHERE root.parent = 0
  AND root.id = 1
ORDER BY childid,grandchildid
)T1

如果你要允许多个级别,你可以创建一个这样的函数sqlFiddle

CREATE FUNCTION getRootId(in_id int) returns int
BEGIN
   DECLARE _parent int;
   DECLARE _id int;
   SET _parent = in_id;
   SET _id = in_id;
   WHILE (_parent != 0) DO
      SELECT id,parent into _id,_parent
      FROM stories WHERE id = _parent;
   END WHILE;
   RETURN _id;
END//

然后使用它来获取所有共享root id的注释,如下所示

SELECT parent.id as parentId,
   parent.type as parentType,
   parent.post as parentPost,
   child.id as childId,
   child.type as childType,
   child.post as childPost,
   getRootId(child.id) as rootId
FROM stories parent
INNER JOIN stories child ON (child.parent = parent.id)
WHERE getRootId(child.id) = 1