I have a categories structure as follows
I want to select leaf node. i mean the the categories who don’t have sub categories.
In my database monitor, cpu, novels and comics will be answer.
Any help will be appreciated.
Edit :
I tried this.
public function get_valid_categories($parent)
{
$has_childs = false;
foreach($this->categories as $key => $value) {
if ($value['parent'] == $parent) {
if ($has_childs === false) {
$has_childs = true;
}
else
{
$this->valid_categories[] = $value['name'];
}
$this->get_valid_categories($key);
}
}
if ($has_childs === true)
{
return $this->valid_categories ;
}
}
and i m calling this function as follows
get_valid_categories(0);
You don’t need a recursive query for this. How about the following SQL:
This takes the rows in your table and self-joins to get each row’s children. It then filters out those rows that have no children by checking for
t2.id is null.