【发布时间】:2013-12-14 22:22:46
【问题描述】:
我的网站有一个评论系统,cmet 存储在一个表格中,如下所示:
post_id | path | total_replies
1 1/ 3
2 1/2/ 2
3 1/2/3/ 1
4 1/2/3/4/ 0
换句话说,post id 2是对post id 1的回复,post id 3是对post id的回复2 是对 post id 1 等的回复。它实际上只是一条路径。
post id 1 的 total_replies 列是 3,因为它有 3 个回复(post id 2、3 和 4),其他行使用相同的逻辑.
因为这是一个评论系统,每次发表新评论时都会添加更多行。我正在尝试做的是找到一种方法来在每次创建新列时增加 total_replies 列。
因此,例如,如果有人对 post id 2 进行了新回复(post id 5),表格将如下所示:
post_id | path | total_replies
1 1/ 4 (this was incremented)
2 1/2/ 3 (this was incremented)
3 1/2/3/ 1
4 1/2/3/4/ 0
5 1/2/5/ 0
所以,我的问题是,每次做出新回复时,我将如何进行递增?
编辑:我当前的查询。
$stmt = $cxn->prepare("UPDATE posts SET total_replies = total_replies+1 WHERE ? LIKE concat(?, '%') AND post_path != ?");
$stmt->bind_param('sss', $new_path, $path, $new_path);
$stmt->execute();
【问题讨论】:
-
这似乎不是存储分层数据的好方法。看看这个:stackoverflow.com/questions/4048151/…
标签: mysql hierarchy hierarchical-data