I have a table like
CommentPaths
============================
ancestor_id | descendant_id
============================
1 1
1 2
1 3
1 4
1 5
1 6
1 7
2 2
2 4
2 5
4 4
5 5
3 3
3 6
3 7
6 6
7 7
intended to represent a tree structure like
1
/ \
2 3
/ \ \
4 5 6
/
7
Suppose I delete 2
. Then I need to delete the records pointed to below.
CommentPaths
============================
ancestor_id | descendant_id
============================
1 1
1 2 <-------------
1 3
1 4
1 5
1 6
1 7
2 2 <-------------
2 4 <-------------
2 5 <-------------
4 4 <-------------
5 5 <-------------
3 3
3 6
3 7
6 6
7 7
In other words, when deleting k
from the tree I need to
- Delete all rows where
ancestor_id
equalsk
- Delete all rows where the
descendant_id
equals thereancestor_id
is one of the ones from 1.
So my query would look something like
SELECT descendant_id FROM CommentPaths WHERE ancestor_id=2 AS descs
DELETE FROM CommentPaths WHERE ancestor_id IN descs
DELETE FROM CommentPaths WHERE descendant_id IN descs
or is there a fancier, more compact way of doing it????