CREATE TABLE `cat_categories` ( `cat_id` int(10) unsigned NOT NULL auto_increment, `cat_parent` int(10) unsigned NOT NULL default '0', `cat_name` char(56) NOT NULL default 'Category', PRIMARY KEY(`cat_id`) ) ENGINE=MyISAM; INSERT INTO `cat_categories` (`cat_id`, `cat_parent`, `cat_name`) VALUES (1, 0, 'Test Category 1'), (2, 0, 'Test Category 2'), (3, 1, 'Test Category 3'), (4, 3, 'Test Category 4'), 

There is a query that creates a table with categories, how in PHP to check the existence of a branch, if we know that

 Test Category 1 -> Test Category 3 -> Test Category 4 

Is it possible not to go through each value, but to form a SQL query that will form this value?

    1 answer 1

    Trees in SQL .

    Maybe this will help you. In general, it is possible to make recursion through PHP. It is a pity in MySQL there is no such feature as

     WITH T ( SELECT * FROM <Table> WHERE <field> = <someCondition> UNION SELECT * FROM <Table> JOIN T ON <SomeCondition> ) 

    In large DBMS such as Oracle , MS SQL this feature solves all problems. In mySQL you will probably have to write a storage.