Querying a self relationship category table

trekkeriii

New Member
I am trying to optimize my php code for the following table\[code\]Create table categories ( cat_id Int UNSIGNED NOT NULL AUTO_INCREMENT, parent_id Int UNSIGNED, cat_name Varchar(50) NOT NULL, Primary Key (cat_id)) ENGINE = InnoDB;\[/code\]To get all categories and subcategories i use one query for querying only parent categories and then issue an individual query to get the sub categories.here is my code for listing the all main categories\[code\]$result = mysql_query("SELECT cat_id, cat_name FROM categories WHERE parent_id IS NULL");\[/code\]To list individual subcategories of each category i use following query\[code\]$sub_result = mysql_query ("SELECT cat_id, cat_name FROM categories WHERE parent_id=$cat_id");\[/code\]I have 30 categories so the above issues 30 queries on each page, I am trying to minimize the number of queries. Any hint?Thanks
 
Back
Top