![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
category_id | name | parent | +-------------+----------------------+--------+ 1 | ELECTRONICS | NULL | 2 | TELEVISIONS | 1 | 3 | TUBE | 2 | 4 | LCD | 2 | 5 | PLASMA | 2 | 6 | PORTABLE ELECTRONICS | 1 | 7 | MP3 PLAYERS | 6 | 8 | FLASH | 7 | 9 | CD PLAYERS | 6 | 10 | 2 WAY RADIOS | 6 | +-------------+----------------------+--------+ |
|
category_id | name | lft | rgt | +-------------+----------------------+-----+-----+ 1 | ELECTRONICS | 1 | 20 | 2 | TELEVISIONS | 2 | 9 | 3 | TUBE | 3 | 4 | 4 | LCD | 5 | 6 | 5 | PLASMA | 7 | 8 | 6 | PORTABLE ELECTRONICS | 10 | 19 | 7 | MP3 PLAYERS | 11 | 14 | 8 | FLASH | 12 | 13 | 9 | CD PLAYERS | 15 | 16 | 10 | 2 WAY RADIOS | 17 | 18 | +-------------+----------------------+-----+-----+ |
#2
| |||
| |||
|
|
I am migrating data from one database schema to another. The old schema has a categorization system based on an adjacency list, with id, category, and parent_id. If one category is under a second, that category has the second's id as its parent id. For example: +-------------+----------------------+--------+ | category_id | name * * * * * * * * | parent | +-------------+----------------------+--------+ | * * * * * 1 | ELECTRONICS * * * * *| * NULL | | * * * * * 2 | TELEVISIONS * * * * *| * * *1 | | * * * * * 3 | TUBE * * * * * * * * | * * *2 | | * * * * * 4 | LCD * * * * * * * * *| * * *2 | | * * * * * 5 | PLASMA * * * * * * * | * * *2 | | * * * * * 6 | PORTABLE ELECTRONICS | * * *1 | | * * * * * 7 | MP3 PLAYERS * * * * *| * * *6 | | * * * * * 8 | FLASH * * * * * * * *| * * *7 | | * * * * * 9 | CD PLAYERS * * * * * | * * *6 | | * * * * *10 | 2 WAY RADIOS * * * * | * * *6 | +-------------+----------------------+--------+ The new schema has a modified preorder tree traversal algorithm: +-------------+----------------------+-----+-----+ | category_id | name * * * * * * * * | lft | rgt | +-------------+----------------------+-----+-----+ | * * * * * 1 | ELECTRONICS * * * * *| * 1 | *20 | | * * * * * 2 | TELEVISIONS * * * * *| * 2 | * 9 | | * * * * * 3 | TUBE * * * * * * * * | * 3 | * 4 | | * * * * * 4 | LCD * * * * * * * * *| * 5 | * 6 | | * * * * * 5 | PLASMA * * * * * * * | * 7 | * 8 | | * * * * * 6 | PORTABLE ELECTRONICS | *10 | *19 | | * * * * * 7 | MP3 PLAYERS * * * * *| *11 | *14 | | * * * * * 8 | FLASH * * * * * * * *| *12 | *13 | | * * * * * 9 | CD PLAYERS * * * * * | *15 | *16 | | * * * * *10 | 2 WAY RADIOS * * * * | *17 | *18 | +-------------+----------------------+-----+-----+ Examples taken from this page:http://dev.mysql.com/tech-resources/...ical-data.html Anywho, I'm capable or writing a php script with a recursive function that will migrate the adjancency list to the preoder tree structure. Basically for each row, it inserts it with a blank 'rgt' value, *looks for children, applies the function recursively to them, keeping track of the counter, and then updates the 'rgt' value. But I want to do this in pure sql. However, I don't know enough to get a foothold on it. For starters, I don't know if you can do this with a recursive query, or if there are other ways of doing it. Can someone help get me started or pointed in the right direction? |
#3
| |||
| |||
|
|
I am migrating data from one database schema to another. The old schema has a categorization system based on an adjacency list, with id, category, and parent_id. If one category is under a second, that category has the second's id as its parent id. For example: +-------------+----------------------+--------+ | category_id | name | parent | +-------------+----------------------+--------+ | 1 | ELECTRONICS | NULL | | 2 | TELEVISIONS | 1 | | 3 | TUBE | 2 | | 4 | LCD | 2 | | 5 | PLASMA | 2 | | 6 | PORTABLE ELECTRONICS | 1 | | 7 | MP3 PLAYERS | 6 | | 8 | FLASH | 7 | | 9 | CD PLAYERS | 6 | | 10 | 2 WAY RADIOS | 6 | +-------------+----------------------+--------+ The new schema has a modified preorder tree traversal algorithm: +-------------+----------------------+-----+-----+ | category_id | name | lft | rgt | +-------------+----------------------+-----+-----+ | 1 | ELECTRONICS | 1 | 20 | | 2 | TELEVISIONS | 2 | 9 | | 3 | TUBE | 3 | 4 | | 4 | LCD | 5 | 6 | | 5 | PLASMA | 7 | 8 | | 6 | PORTABLE ELECTRONICS | 10 | 19 | | 7 | MP3 PLAYERS | 11 | 14 | | 8 | FLASH | 12 | 13 | | 9 | CD PLAYERS | 15 | 16 | | 10 | 2 WAY RADIOS | 17 | 18 | +-------------+----------------------+-----+-----+ |
#4
| |||||
| |||||
|
|
Every time you add a *new category you must potentially shift your lft/ rht min and max values or spend time trying to "redo" what you already have. |

|
What happens when you overrun your allotment? *Do you then re- order your entire database. |
|
It appears that you are trying to solve a problem that does not exist. |
|
Take a step back and ask yourself exactly what problem are you trying to solve? * |
|
Any time a table must self-reference itself, it is most often a disaster in the making. * |
#5
| ||||
| ||||
|
|
FROM language_family_adj_list AS t1 LEFT JOIN language_family_adj_list AS t2 ON t2.parent_id = t1.language_id LEFT JOIN language_family_adj_list AS t3 ON t3.parent_id = t2.language_id LEFT JOIN language_family_adj_list AS t4 ON t4.parent_id = t3.language_id LEFT JOIN language_family_adj_list AS t5 ON t5.parent_id = t4.language_id WHERE t1.parent_id IS NULL ORDER BY t1.language, t2.language, t3.language, t4.language, t5.language; |
|
lev1 | lev2 | lev3 | lev4 | lev5 +-------------+---------------+--------------+------------------+------ |
|
Finno-Ugric | Baltic-Finnic | Estonian | South Estonian | Voro Finno-Ugric | Baltic-Finnic | Finnish | NULL | NULL Finno-Ugric | Baltic-Finnic | Ingrian | NULL | NULL Finno-Ugric | Baltic-Finnic | Karelian | Karelian Proper | NULL Finno-Ugric | Baltic-Finnic | Karelian | Lude | NULL Finno-Ugric | Baltic-Finnic | Karelian | Olonets Karelian | NULL Finno-Ugric | Baltic-Finnic | Livonian | NULL | NULL Finno-Ugric | Baltic-Finnic | Veps | NULL | NULL Finno-Ugric | Baltic-Finnic | Votic | NULL | NULL Finno-Ugric | Hungarian | NULL | NULL | NULL Finno-Ugric | Khanty | NULL | NULL | NULL Finno-Ugric | Mansi | NULL | NULL | NULL Finno-Ugric | Mari | NULL | NULL | NULL Finno-Ugric | Mordvinic | Erzya | NULL | NULL Finno-Ugric | Mordvinic | Moksha | NULL | NULL Finno-Ugric | Permic | Komi | NULL | NULL Finno-Ugric | Permic | Komi-Permyak | NULL | NULL Finno-Ugric | Permic | Udmurt | NULL | NULL Finno-Ugric | Sami | Eastern Sami | Akkala Sami | NULL Finno-Ugric | Sami | Eastern Sami | Inari Sami | NULL Finno-Ugric | Sami | Eastern Sami | Kemi Sami | NULL Finno-Ugric | Sami | Eastern Sami | Kildin Sami | NULL Finno-Ugric | Sami | Eastern Sami | Skolt Sami | NULL Finno-Ugric | Sami | Eastern Sami | Ter Sami | NULL Finno-Ugric | Sami | Western Sami | Lule Sami | NULL Finno-Ugric | Sami | Western Sami | Northern Sami | NULL Finno-Ugric | Sami | Western Sami | Pite Sami | NULL Finno-Ugric | Sami | Western Sami | Southern Sami | NULL Finno-Ugric | Sami | Western Sami | Umi Sami | NULL +-------------+---------------+--------------+------------------+------ |
|
name | +---------------------------------+ Finno-Ugric | Baltic-Finnic | Estonian | South Estonian | Voro | Finnish | Ingrian | Karelian | Karelian Proper | Lude | Olonets Karelian | Livonian | Veps | Votic | Hungarian | Khanty | Mansi | Mari | Mordvinic | Erzya | Moksha | Permic | Komi | Komi-Permyak | Udmurt | Sami | Eastern Sami | Akkala Sami | Inari Sami | Kemi Sami | Kildin Sami | Skolt Sami | Ter Sami | Western Sami | Lule Sami | Northern Sami | Pite Sami | Southern Sami | Umi Sami | +---------------------------------+ |

![]() |
| Thread Tools | |
| Display Modes | |
| |