Answer the question
In order to leave comments, you need to log in
Storing trees in Mysql what are the ways?
I need to store a tree in the database, and it is necessary that the sequence can be changed.
those. sorting was possible.
Something like this
Математика
- Теория вероятностей
- Логика
Юриспруденция
- Административное право
- Трудовое право
Язык
- Русский
- Английский
- Испанский
Answer the question
In order to leave comments, you need to log in
There are 4 main ways to store trees in a relational database:
Adjacency List - described by @Satanpit .
Matherialized Path - the full path to the node is stored as a string like 0035.0993.7324.1256 (i.e. the top-level node contains the string 0035, the second-level node contains the string 0035.0993, etc.)
Nested Set - 2 numbers are stored for each node: the left and right indices. Moreover, the left index of the node is less than the left index of any descendant of this node, and the right index is correspondingly greater.
Closure Table - store data separately, separate hierarchy.
There are also combinations of these methods.
For all these methods in RuNet (including Habré) there is a large amount of information.
The database structure is something like this:
To select in PHP, use recursion.
Examples in the network shaft
Joe Celko has an excellent book . In general, he is the author of many very good books on SQL.
Read towards the Nested Set. Here is a small article on Habré
habrahabr.ru/post/153861
I studied this www.getinfo.ru/article610.html
You can look for ready-made extensions if you write in a framework. Here is a nice one for Yii
https://github.com/yiiext/nested-set-behavior
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question