Home > Back-end >  About id generated from the beginning of the 0001, please, help
About id generated from the beginning of the 0001, please, help

Time:05-14

Table, there is now a department generate a data since 0001, when if the node is brother is 0002 so on, if is the child node is 00010001 and so on,,,
Could you tell me how to implement this to

CodePudding user response:

Table id code, parent_id
Id is the primary key id, id code is 0001 this, parent_id is father
Defines the id of 1 the top department, of course, this department can not inventory, this has to do with interaction, add the department didn't select the parent department, then think this department parent_id to 1
Also is the code for the department of 0001, you said that of course the parent_id to 1 logic can do front end, also can do the back-end, suggest the back-end do
Code is not to say, the simplified into SQL so to write
Same rank first to find out how many department
Select count (1) the as dept_count from dept where parent_id=# {parent_id}
And then find out the parent department code, if parent_id is 1 for "" directly by the
Select code as parent_code from dept where id=# {parent_id}
Then insert
Insert into dept (id, code, parent_id) values (next_id, CONCAT (# {parent_code}, Lpad (# {dept_count} + 1, 4, '0')), # {parent_id})
Probably so. I hope you can understand this, and suggested the only key code