For example, I want to get the final parent class of the little black pig with ID 10. In the table, the parent_id is 9, but what I want to get is 5. Is there any way, or I want to judge a certain item? Record whether it belongs to the final parent class
Do a recursive search, and then find the one when parent_id=null
There are 2 methods you can try:
Query all ids and parent_id, and then search, so that fixed sql statements can be cached.
Add a new field root_id to record the root node, so there is no need to search, just query it directly. You only need to query it once when inserting.