mysql是否可以在视图

时间:2018-02-17 13:07:18

标签: mysql sql

我正在尝试为评论表创建一个视图。在其中一个单元格中,它会计算评论的评论量。这就是原始表的外观

+--+-------------------------------------+----------------+----------------+
|id|comments                             |commentOnContent|commentOnComment|
+--+-------------------------------------+----------------+----------------+
|1 |this is a comment                    |24              |0               |
+--+-------------------------------------+----------------+----------------+
|2 |this is a reply to that comment      |0               |1               |
+--+-------------------------------------+----------------+----------------+
|3 |this is another reply to that comment|0               |1               |
+--+-------------------------------------+----------------+----------------+
|4 |this is a reply to a reply           |0               |3               |
+--+-------------------------------------+----------------+----------------+

这是视图的外观

+--+-------------------------------------+-------+----------------+----------------+
|id|comments                             |replies|commentOnContent|commentOnComment|
+--+-------------------------------------+-------+----------------+----------------+
|1 |this is a comment                    |0      |24              |0               |
+--+-------------------------------------+-------+----------------+----------------+
|2 |this is a reply to that comment      |0      |0               |1               |
+--+-------------------------------------+-------+----------------+----------------+
|3 |this is another reply to that comment|0      |0               |1               |
+--+-------------------------------------+-------+----------------+----------------+
|4 |this is a reply to a reply           |0      |0               |3               |
+--+-------------------------------------+-------+----------------+----------------+

对于id为1的评论,回复单元格应该等于3,因为它有两个回复以及对其中一个回复的回复。我可以通过一个简单的陈述select count(*) from comments where commentOnComment = id;看到它有多少回复。但是,如果我想在其回复中添加回复的数量和对其回复的回复我假设我必须做某种递归函数但是你们中的一些人可能知道问题是存储的函数可以是在视图中但不能递归,存储过程可以递归但不能在视图中。

简而言之,问题是,是否有任何可能的方法来使用递归语句来解决每个评论有多少回复和回复回复的问题?

0 个答案:

没有答案