How to find the child and subchild of particular node in a tree in sql.
On Sat, Jul 5, 2008 at 4:42 PM, mrbless [email protected] wrote:
How to find the child and subchild of particular node in a tree in sql.
more data needed for intelligent answer…
–
Rails, RSpec and Life blog…
Thanks I have resolve it.
my database is like this:
{
id
name
subject
body
root_id
parent_id
lft
rgt
depth
created_at
updated_at
}
now what I want to do for the given id is I want to find all its
children and sub children
I think this work
@posts=ForumPost.find_by_sql("SELECT parent.* "+
"FROM forum_posts AS node, "+
"forum_posts AS parent "+
"WHERE node.lft<=parent.lft AND node.rgt>=parent.rgt "+
“AND parent.root_id=node.root_id AND node.parent_id!=0 AND
node.parent_id =”+params[:id])
===========
Tell me guys whether this is absolutely fine or not as I haven’t
checked it for all cases:
@post=ForumPost.find_by_sql("SELECT parent.* " +
"FROM forum_posts AS node, “+
“forum_posts AS parent “+
“WHERE (node.lft<=parent.lft AND node.rgt>=parent.rgt " +
“AND node.parent_id =”+params[:id]+
" AND parent.root_id=node.root_id AND node.parent_id!=0) or
(parent.id=”+params[:id]+ " AND node.id=”+params[:id] +”)”)
========================