Re: Recursion with database tables
| From: | David Otton | Date: | Wed, 19 Feb 2003 13:42:42 +0000 |
| Subject: | Re: Recursion with database tables | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-136173@lists.php.net to get a copy of this message | ||
On Wed, 19 Feb 2003 14:16:47 +0000, you wrote:
>>Otherwise, yes, in MySQL you have to recurse down the tree deleting
>>comments.
>How would I go about recursing down the tree? Has anyone done this before?
I have, but it's been a while. Something like (pseudo-code) .
def delete_comments(a) :
for b in a :
c = "SELECT commentid FROM comments WHERE parentcommentid = a"
if len(c) > 0 :
delete_comments(c)
"DELETE FROM comments WHERE commentid IN (a)"
return
def main() :
a = "SELECT commentid FROM comments WHERE topicid = 1234"
delete_comments(a)
"DELETE FROM topic WHERE topicid = 1234"
where a is a list of commentids, b is a single commentid and c is a list
of child commentids. This will delete the tree in a depth-first fashion.
This is quite an intensive way of doing things (potentially many selects
and deletes) but is just-about acceptable because you probably will
delete an entire topic so infrequently.
I recommend filling out the topicid on every post as the easier
solution, though.