我的网站具有评论系统,并且评论存储在如下表中:
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 2]的答复id 1等。实际上,这只是一条路径。
[[post id 1的total_replies列为3
,因为对它进行了3次答复(post id 2、3和4),其他行使用了相同的逻辑。
例如,如果有人对
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
所以,我的问题是,每次收到新答复时,我将如何递增?:我当前的查询。EDIT
$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();
update
和where
子句执行所需的操作:update comments
set total_replies = total_replies + 1
where NEWPATH like concat(path, '%');
在这种情况下,NEWPATH将为'1/2/5/'
。尚不清楚是将其包含在变量中还是仅作为表中的新行使用。假设您将其作为变量。编辑:
如果您不想更新原始记录:
update comments set total_replies = total_replies + 1 where NEWPATH like concat(path, '%') and NEWPATH <> path;