Could you tell me how to implement this to
CodePudding user response:
Table id code, parent_idId 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