CREATE TABLE person(
id VARCHAR(255) NOT NULL COMMENT 'ID' ,
name VARCHAR(255) COMMENT '名称' ,
parent_id VARCHAR(255) COMMENT '父ID' ,
PRIMARY KEY (id)
) COMMENT = '人员组织架构';
INSERT INTO `person` (`id`, `name`, `parent_id`) VALUES ('1', '北京子公司', '0');
INSERT INTO `person` (`id`, `name`, `parent_id`) VALUES ('2', '采购部', '1');
INSERT INTO `person` (`id`, `name`, `parent_id`) VALUES ('3', '采购', '2');
INSERT INTO `person` (`id`, `name`, `parent_id`) VALUES ('4', '仓储', '2');
INSERT INTO `person` (`id`, `name`, `parent_id`) VALUES ('5', '物流', '2');
INSERT INTO `person` (`id`, `name`, `parent_id`) VALUES ('6', '采购一组', '3');
INSERT INTO `person` (`id`, `name`, `parent_id`) VALUES ('7', '采购二组', '3');
INSERT INTO `person` (`id`, `name`, `parent_id`) VALUES ('8', '张三', '6');
INSERT INTO `person` (`id`, `name`, `parent_id`) VALUES ('9', '李四', '6');
INSERT INTO `person` (`id`, `name`, `parent_id`) VALUES ('10', '王五', '7');
(北京子公司增加财务部)
INSERT INTO `person` (`id`, `name`, `parent_id`)
VALUES (11, '财务部', '1');
INSERT INTO `person` (`id`, `name`, `parent_id`)
VALUES (12, '财务经理', '11');
INSERT INTO `person` (`id`, `name`, `parent_id`)
VALUES (13, '财务助理', '11');
(北京子公司删除财务部及下属职员)
(采购 下属全部人员)
WITH RECURSIVE _children AS
(
SELECT fun.* FROM `person` fun WHERE fun.name='采购'
UNION ALL
SELECT fun.* FROM _children,`person` fun WHERE fun.parent_id=_children.id
)
SELECT * FROM _children;
(采购 直属部门或员工)
SELECT * FROM `person` a WHERE a.parent_id in
(
SELECT id FROM `person` b WHERE b.name='采购'
);
(采购一组并入仓储)