I have this table design.I want to get all his children in specified parentid,but I am having problem in how to filter and get all his donwline children
CREATE TABLE `treetbl` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`parentid` INT(11) NOT NULL DEFAULT '0',
`lft` INT(11) NOT NULL DEFAULT '0',
`rgt` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
treetbl
id parentid lft rgt
1 1 1 1
2 5 6 7
3 7 8 9
Here is my query to Retrieving a Full Tree
SELECT node.parentid,node.lft,node.rgt
FROM treetbl AS node,
treetbl AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
ORDER BY node.lft;
but I am confuse how to filter the parentid example 5 to get all his down children.
Thank you in advance.
If I understand you correctly, you want to select only the records that have parentid = 5
. Am I right?
If so and you say that this works for you with all entries:
SELECT node.parentid,node.lft,node.rgt FROM treetbl AS node, treetbl AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt ORDER BY node.lft;
, then why not just add parentid = 5
with a help of AND
statement to it? Like this:
SELECT node.parentid,node.lft,node.rgt FROM treetbl AS node, treetbl AS parent
WHERE parentid = 5 AND node.lft BETWEEN parent.lft AND parent.rgt
ORDER BY node.lft;
Of course, you can change that hard-coded 5
to some $parentID
PHP variable.
maybe this is what you're looking for
SELECT * FROM (SELECT node.parentid,node.lft,node.rgt
FROM treetbl AS node,
treetbl AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
ORDER BY node.lft)A Where A.parentid>=5 ;
let me know if I am wrong