MySQL Forums
Forum List  »  Newbie

recursive query in a product catalog tree?
Posted by: peter michaux
Date: August 05, 2005 07:04PM

I have a catalog like the following

CREATE TABLE category (
category_id INTEGER(10) UNSIGNED NOT NULL auto_increment,
parent_category_id int(10) unsigned NOT NULL default '0',
PRIMARY KEY (category_id)
) ENGINE=innoDB;

CREATE TABLE product (
product_id INTEGER(10) UNSIGNED NOT NULL auto_increment,
name varchar(10) NOT NULL default '',
PRIMARY KEY (product_id)
) ENGINE=innoDB;

# a product can be in multiple categories
CREATE TABLE category_product (
category_id INTEGER(10) UNSIGNED NOT NULL default '0',
product_id INTEGER(10) UNSIGNED NOT NULL default '0',
PRIMARY KEY (category_id, product_id)
) ENGINE=innoDB;

With only one query I would like to find all the categories that a product is in up the tree to a top level category with parent_category_id==0. To me this seems like a recursive search up the tree. The only way I can think to do it is to get the categories that a product is in and then for each one follow the tree up step buy step. But this takes many queries and a loop in PHP.

Is there a single step query that will do this?

Thanks,
Peter

Options: ReplyQuote


Subject
Written By
Posted
recursive query in a product catalog tree?
August 05, 2005 07:04PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.