mysql> SELECT node.nameFROM nested_category AS node, nested_category AS parentWHERE node.lft BETWEEN parent.lft AND parent.rgt AND parent.name = 'ELECTRONICS'ORDER BY node.lft;+----------------------+| name |+----------------------+| ELECTRONICS || TELEVISIONS || TUBE || LCD || PLASMA || PORTABLE ELECTRONICS || MP3 PLAYERS || FLASH || CD PLAYERS || 2 WAY RADIOS |+----------------------+10 rows in setmysql> SELECT nameFROM nested_categoryWHERE rgt = lft + 1;+--------------+| name |+--------------+| TUBE || LCD || PLASMA || FLASH || CD PLAYERS || 2 WAY RADIOS |+--------------+6 rows in setmysql> SELECT parent.nameFROM nested_category AS node, nested_category AS parentWHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.name = 'FLASH'ORDER BY parent.lft ASC;+----------------------+| name |+----------------------+| ELECTRONICS || PORTABLE ELECTRONICS || MP3 PLAYERS || FLASH |+----------------------+4 rows in setmysql> SELECT node.name, (COUNT(parent.name) - 1) AS depthFROM nested_category AS node, nested_category AS parentWHERE node.lft BETWEEN parent.lft AND parent.rgtGROUP BY node.nameORDER BY node.lft;+----------------------+-------+| name | depth |+----------------------+-------+| ELECTRONICS | 0 || TELEVISIONS | 1 || TUBE | 2 || LCD | 2 || PLASMA | 2 || PORTABLE ELECTRONICS | 1 || MP3 PLAYERS | 2 || FLASH | 3 || CD PLAYERS | 2 || 2 WAY RADIOS | 2 |+----------------------+-------+10 rows in setmysql> SELECT CONCAT( REPEAT('|--', COUNT(parent.name) - 1), node.name) AS nameFROM nested_category AS node, nested_category AS parentWHERE node.lft BETWEEN parent.lft AND parent.rgtGROUP BY node.nameORDER BY node.lft;+-------------------------+| name |+-------------------------+| ELECTRONICS || |--TELEVISIONS || |--|--TUBE || |--|--LCD || |--|--PLASMA || |--PORTABLE ELECTRONICS || |--|--MP3 PLAYERS || |--|--|--FLASH || |--|--CD PLAYERS || |--|--2 WAY RADIOS |+-------------------------+10 rows in setmysql> SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depthFROM nested_category AS node, nested_category AS parent, nested_category AS sub_parent, ( SELECT node.name, (COUNT(parent.name) - 1) AS depth FROM nested_category AS node, nested_category AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.name = 'PORTABLE ELECTRONICS' GROUP BY node.name ORDER BY node.lft )AS sub_treeWHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt AND sub_parent.name = sub_tree.nameGROUP BY node.nameORDER BY node.lft;+----------------------+-------+| name | depth |+----------------------+-------+| PORTABLE ELECTRONICS | 0 || MP3 PLAYERS | 1 || FLASH | 2 || CD PLAYERS | 1 || 2 WAY RADIOS | 1 |+----------------------+-------+5 rows in setmysql> SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depthFROM nested_category AS node, nested_category AS parent, nested_category AS sub_parent, ( SELECT node.name, (COUNT(parent.name) - 1) AS depth FROM nested_category AS node, nested_category AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.name = 'PORTABLE ELECTRONICS' GROUP BY node.name ORDER BY node.lft )AS sub_treeWHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt AND sub_parent.name = sub_tree.nameGROUP BY node.nameORDER BY node.lft;+----------------------+-------+| name | depth |+----------------------+-------+| PORTABLE ELECTRONICS | 0 || MP3 PLAYERS | 1 || FLASH | 2 || CD PLAYERS | 1 || 2 WAY RADIOS | 1 |+----------------------+-------+5 rows in setmysql> SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depthFROM nested_category AS node, nested_category AS parent, nested_category AS sub_parent, ( SELECT node.name, (COUNT(parent.name) - 1) AS depth FROM nested_category AS node, nested_category AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.name = 'PORTABLE ELECTRONICS' GROUP BY node.name ORDER BY node.lft )AS sub_treeWHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt AND sub_parent.name = sub_tree.nameGROUP BY node.nameHAVING depth <= 1ORDER BY node.lft;+----------------------+-------+| name | depth |+----------------------+-------+| PORTABLE ELECTRONICS | 0 || MP3 PLAYERS | 1 || CD PLAYERS | 1 || 2 WAY RADIOS | 1 |+----------------------+-------+4 rows in setmysql> SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depthFROM nested_category AS node, nested_category AS parent, nested_category AS sub_parent, ( SELECT node.name, (COUNT(parent.name) - 1) AS depth FROM nested_category AS node, nested_category AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.name = 'PORTABLE ELECTRONICS' GROUP BY node.name ORDER BY node.lft )AS sub_treeWHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt AND sub_parent.name = sub_tree.nameGROUP BY node.nameHAVING depth = 1ORDER BY node.lft;+--------------+-------+| name | depth |+--------------+-------+| MP3 PLAYERS | 1 || CD PLAYERS | 1 || 2 WAY RADIOS | 1 |+--------------+-------+3 rows in setmysql> CREATE TABLE product( product_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(40), category_id INT NOT NULL);INSERT INTO product(name, category_id) VALUES('20" TV',3),('36" TV',3),('Super-LCD 42"',4),('Ultra-Plasma 62"',5),('Value Plasma 38"',5),('Power-MP3 5gb',7),('Super-Player 1gb',8),('Porta CD',9),('CD To go!',9),('Family Talk 360',10);Query OK, 0 rows affectedQuery OK, 10 rows affectedRecords: 10 Duplicates: 0 Warnings: 0mysql> SELECT parent.name, COUNT(product.name)FROM nested_category AS node , nested_category AS parent, productWHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.category_id = product.category_idGROUP BY parent.nameORDER BY node.lft;+----------------------+---------------------+| name | COUNT(product.name) |+----------------------+---------------------+| ELECTRONICS | 10 || TELEVISIONS | 5 || TUBE | 2 || LCD | 1 || PLASMA | 2 || PORTABLE ELECTRONICS | 5 || MP3 PLAYERS | 2 || FLASH | 1 || CD PLAYERS | 2 || 2 WAY RADIOS | 1 |+----------------------+---------------------+10 rows in setmysql> LOCK TABLE nested_category WRITE;SELECT @myRight := rgt FROM nested_categoryWHERE name = 'TELEVISIONS';UPDATE nested_category SET rgt = rgt + 2 WHERE rgt > @myRight;UPDATE nested_category SET lft = lft + 2 WHERE lft > @myRight;INSERT INTO nested_category(name, lft, rgt) VALUES('GAME CONSOLES', @myRight + 1, @myRight + 2);UNLOCK TABLES;Query OK, 0 rows affected+-----------------+| @myRight := rgt |+-----------------+| 9 |+-----------------+1 row in setQuery OK, 6 rows affectedRows matched: 6 Changed: 6 Warnings: 0Query OK, 5 rows affectedRows matched: 5 Changed: 5 Warnings: 0Query OK, 1 row affectedQuery OK, 0 rows affectedmysql> SELECT CONCAT( REPEAT('|--', COUNT(parent.name) - 1), node.name) AS nameFROM nested_category AS node, nested_category AS parentWHERE node.lft BETWEEN parent.lft AND parent.rgtGROUP BY node.nameORDER BY node.lft;+-------------------------+| name |+-------------------------+| ELECTRONICS || |--TELEVISIONS || |--|--TUBE || |--|--LCD || |--|--PLASMA || |--GAME CONSOLES || |--PORTABLE ELECTRONICS || |--|--MP3 PLAYERS || |--|--|--FLASH || |--|--CD PLAYERS || |--|--2 WAY RADIOS |+-------------------------+11 rows in setmysql> LOCK TABLE nested_category WRITE;SELECT @myLeft := lft FROM nested_categoryWHERE name = '2 WAY RADIOS';UPDATE nested_category SET rgt = rgt + 2 WHERE rgt > @myLeft;UPDATE nested_category SET lft = lft + 2 WHERE lft > @myLeft;INSERT INTO nested_category(name, lft, rgt) VALUES('FRS', @myLeft + 1, @myLeft + 2);UNLOCK TABLES;Query OK, 0 rows affected+----------------+| @myLeft := lft |+----------------+| 19 |+----------------+1 row in setQuery OK, 3 rows affectedRows matched: 3 Changed: 3 Warnings: 0Query OK, 0 rows affectedRows matched: 0 Changed: 0 Warnings: 0Query OK, 1 row affectedQuery OK, 0 rows affectedmysql> SELECT CONCAT( REPEAT('|--', COUNT(parent.name) - 1), node.name) AS nameFROM nested_category AS node, nested_category AS parentWHERE node.lft BETWEEN parent.lft AND parent.rgtGROUP BY node.nameORDER BY node.lft;+-------------------------+| name |+-------------------------+| ELECTRONICS || |--TELEVISIONS || |--|--TUBE || |--|--LCD || |--|--PLASMA || |--GAME CONSOLES || |--PORTABLE ELECTRONICS || |--|--MP3 PLAYERS || |--|--|--FLASH || |--|--CD PLAYERS || |--|--2 WAY RADIOS || |--|--|--FRS |+-------------------------+12 rows in setmysql> LOCK TABLE nested_category WRITE;SELECT @myLeft := lft, @myRight := rgt, @myWidth := rgt - lft + 1FROM nested_categoryWHERE name = 'GAME CONSOLES';DELETE FROM nested_category WHERE lft BETWEEN @myLeft AND @myRight;UPDATE nested_category SET rgt = rgt - @myWidth WHERE rgt > @myRight;UPDATE nested_category SET lft = lft - @myWidth WHERE lft > @myRight;UNLOCK TABLES;Query OK, 0 rows affected+----------------+-----------------+---------------------------+| @myLeft := lft | @myRight := rgt | @myWidth := rgt - lft + 1 |+----------------+-----------------+---------------------------+| 10 | 11 | 2 |+----------------+-----------------+---------------------------+1 row in setQuery OK, 1 row affectedQuery OK, 7 rows affectedRows matched: 7 Changed: 7 Warnings: 0Query OK, 6 rows affectedRows matched: 6 Changed: 6 Warnings: 0Query OK, 0 rows affectedmysql> SELECT CONCAT( REPEAT('|--', COUNT(parent.name) - 1), node.name) AS nameFROM nested_category AS node, nested_category AS parentWHERE node.lft BETWEEN parent.lft AND parent.rgtGROUP BY node.nameORDER BY node.lft;+-------------------------+| name |+-------------------------+| ELECTRONICS || |--TELEVISIONS || |--|--TUBE || |--|--LCD || |--|--PLASMA || |--PORTABLE ELECTRONICS || |--|--MP3 PLAYERS || |--|--|--FLASH || |--|--CD PLAYERS || |--|--2 WAY RADIOS || |--|--|--FRS |+-------------------------+11 rows in setmysql> LOCK TABLE nested_category WRITE;SELECT @myLeft := lft, @myRight := rgt, @myWidth := rgt - lft + 1FROM nested_categoryWHERE name = 'MP3 PLAYERS';DELETE FROM nested_category WHERE lft BETWEEN @myLeft AND @myRight;UPDATE nested_category SET rgt = rgt - @myWidth WHERE rgt > @myRight;UPDATE nested_category SET lft = lft - @myWidth WHERE lft > @myRight;UNLOCK TABLES;Query OK, 0 rows affected+----------------+-----------------+---------------------------+| @myLeft := lft | @myRight := rgt | @myWidth := rgt - lft + 1 |+----------------+-----------------+---------------------------+| 11 | 14 | 4 |+----------------+-----------------+---------------------------+1 row in setQuery OK, 2 rows affectedQuery OK, 5 rows affectedRows matched: 5 Changed: 5 Warnings: 0Query OK, 3 rows affectedRows matched: 3 Changed: 3 Warnings: 0Query OK, 0 rows affectedmysql> SELECT CONCAT( REPEAT('|--', COUNT(parent.name) - 1), node.name) AS nameFROM nested_category AS node, nested_category AS parentWHERE node.lft BETWEEN parent.lft AND parent.rgtGROUP BY node.nameORDER BY node.lft;+-------------------------+| name |+-------------------------+| ELECTRONICS || |--TELEVISIONS || |--|--TUBE || |--|--LCD || |--|--PLASMA || |--PORTABLE ELECTRONICS || |--|--CD PLAYERS || |--|--2 WAY RADIOS || |--|--|--FRS |+-------------------------+9 rows in setmysql> LOCK TABLE nested_category WRITE;SELECT @myLeft := lft, @myRight := rgt, @myWidth := rgt - lft + 1FROM nested_categoryWHERE name = 'PORTABLE ELECTRONICS';DELETE FROM nested_category WHERE lft = @myLeft;UPDATE nested_category SET rgt = rgt - 1, lft = lft - 1 WHERE lft BETWEEN @myLeft AND @myRight;UPDATE nested_category SET rgt = rgt - 2 WHERE rgt > @myRight;UPDATE nested_category SET lft = lft - 2 WHERE lft > @myRight;UNLOCK TABLES;Query OK, 0 rows affected+----------------+-----------------+---------------------------+| @myLeft := lft | @myRight := rgt | @myWidth := rgt - lft + 1 |+----------------+-----------------+---------------------------+| 10 | 17 | 8 |+----------------+-----------------+---------------------------+1 row in setQuery OK, 1 row affectedQuery OK, 3 rows affectedRows matched: 3 Changed: 3 Warnings: 0Query OK, 1 row affectedRows matched: 1 Changed: 1 Warnings: 0Query OK, 0 rows affectedRows matched: 0 Changed: 0 Warnings: 0Query OK, 0 rows affectedmysql> SELECT CONCAT( REPEAT('|--', COUNT(parent.name) - 1), node.name) AS nameFROM nested_category AS node, nested_category AS parentWHERE node.lft BETWEEN parent.lft AND parent.rgtGROUP BY node.nameORDER BY node.lft;+-----------------+| name |+-----------------+| ELECTRONICS || |--TELEVISIONS || |--|--TUBE || |--|--LCD || |--|--PLASMA || |--CD PLAYERS || |--2 WAY RADIOS || |--|--FRS |+-----------------+8 rows in setmysql> SELECT @myLeft := lft, @myRight := rgt, @myWidth := rgt - lft + 1FROM nested_categoryWHERE name = 'LCD';+----------------+-----------------+---------------------------+| @myLeft := lft | @myRight := rgt | @myWidth := rgt - lft + 1 |+----------------+-----------------+---------------------------+| 5 | 6 | 2 |+----------------+-----------------+---------------------------+1 row in setmysql>