How to use php to get the sub-classification, parent classification and sibling classification of a piece of data?

for example, the database structure is
id fid name three fields
1 name1
21 name2
3 1 name3
4 3 name4
5 3 name5

for example, if you are currently dealing with data with an id of 3, how can you query and output all the relevant peer, parent, and child structures?
means to query all the structures of the top-level directory of this data all the way to the lowest-level directory to
to find a more resource-saving and faster method

Php
Mar.25,2021

it is recommended to add a new field to indicate the path

id fid name path
1 0 name1 1,
21 name2 1 name3 2,
3 1 name3 1 name4 3,
4 3 name4 1 penny 3,
5 3 name5 1 Person3,

query select * from xx where path like '1Magi%'
query select * from xx where path like '1Magi% 2 under query 2


Ah, I've seen the new rating table design before, and I forgot the name of the link, so I'll tell you the design plan directly.
field: id (primary key) top_id (original ID) pid (parent ID) name (content) lv_num (level)
. Now that you see here, you can think about the pros and cons of this design. If you can figure it out directly, it's best. If you can't understand it, please see the following:

each category is managed by recording three fields, namely ancestor ID top_id, which is the highest level category for recording the data, while pid records the parent category, and lv_num records the level to which the current classification belongs. In this way, if you want to find all the data under the corresponding category, you only need to get all the data directly through top_id, and if you only want to get the next level, you can get it through pid. If you want to find a certain level, you can get it through lv_num. If you want a sibling under a category, you can do it with pid and lv_num. Finally, if you want to find all the classifications at the level below a large classification, you can do so through top_id and lv_num.

you can compare the advantages and disadvantages of other designs.

Menu