http://blog.csdn.net/moyanxuan_1993_2_24/article/details/52790314
目录结构:
DROP TABLE IF EXISTS
vrv_org_tab
;
CREATE TABLEvrv_org_tab
(id
bigint(8) NOT NULL AUTO_INCREMENT,org_name
varchar(50) NOT NULL,org_level
int(4) NOT NULL DEFAULT ‘0’,org_parent_id
bigint(8) NOT NULL DEFAULT ‘0’,
PRIMARY KEY (id
),
UNIQUE KEYunique_org_name
(org_name
)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
INSERT INTO
vrv_org_tab
VALUES (‘1’, ‘北信源’, ‘1’, ‘0’);
INSERT INTOvrv_org_tab
VALUES (‘2’, ‘北京’, ‘2’, ‘1’);
INSERT INTOvrv_org_tab
VALUES (‘3’, ‘南京’, ‘2’, ‘1’);
INSERT INTOvrv_org_tab
VALUES (‘4’, ‘武汉’, ‘2’, ‘1’);
INSERT INTOvrv_org_tab
VALUES (‘5’, ‘上海’, ‘2’, ‘1’);
INSERT INTOvrv_org_tab
VALUES (‘6’, ‘北京研发中心’, ‘3’, ‘2’);
INSERT INTOvrv_org_tab
VALUES (‘7’, ‘南京研发中心’, ‘3’, ‘3’);
INSERT INTOvrv_org_tab
VALUES (‘8’, ‘武汉研发中心’, ‘3’, ‘4’);
INSERT INTOvrv_org_tab
VALUES (‘9’, ‘上海研发中心’, ‘3’, ‘5’);
INSERT INTOvrv_org_tab
VALUES (‘10’, ‘北京EMM项目组’, ‘4’, ‘6’);
INSERT INTOvrv_org_tab
VALUES (‘11’, ‘北京linkdd项目组’, ‘4’, ‘6’);
INSERT INTOvrv_org_tab
VALUES (‘12’, ‘南京EMM项目组’, ‘4’, ‘7’);
INSERT INTOvrv_org_tab
VALUES (‘13’, ‘南京linkdd项目组’, ‘4’, ‘7’);
INSERT INTOvrv_org_tab
VALUES (‘14’, ‘武汉EMM项目组’, ‘4’, ‘8’);
INSERT INTOvrv_org_tab
VALUES (‘15’, ‘武汉linkdd项目组’, ‘4’, ‘8’);
INSERT INTOvrv_org_tab
VALUES (‘16’, ‘上海EMM项目组’, ‘4’, ‘9’);
INSERT INTOvrv_org_tab
VALUES (‘17’, ‘上海linkdd项目组’, ‘4’, ‘9’);
create function getChildrenOrg(orgid INT) returns varchar(4000) BEGIN DECLARE oTemp VARCHAR(4000);
DECLARE oTempChild VARCHAR(4000);
SET oTemp = '';
SET oTempChild = CAST(orgid AS CHAR);
WHILE oTempChild IS NOT NULL
DO SET oTemp = CONCAT(oTemp,',',oTempChild);
SELECT GROUP_CONCAT(id) INTO oTempChild FROM vrv_org_tab WHERE FIND_IN_SET(org_parent_id,oTempChild) > 0;
END WHILE;
RETURN oTemp;
END
根据子id查询父节点就不那么麻烦了,不需要写递归函数,当然,你也可以写递归函数来查询。我这边提供的是不写函数的方式。请看代码
SELECT id,org_name,org_level,org_parent_id FROM ( SELECT @r AS _id, (SELECT @r := org_parent_id FROM vrv_org_tab WHERE id = _id) AS parent_id, @l := @l + 1 AS lvl FROM (SELECT @r := 10000, @l := 0) vars, vrv_org_tab h WHERE @r <> 0) T1 JOIN vrv_org_tab T2 ON T1._id = T2.id ORDER BY id;
注意:大家看到那个10000了吗,就是我们的子节点id。
注意:只支持单个查询,意思是不可以根据两个或者两个以上的子节点同时查询出所有父节点。我们可以看到,上面参数都是单个值进行递归查询的。
西面提供一个函数支持多个查询
该功能常用于组织机构模糊搜索
CREATE FUNCTION getParentOrgByOrgName(orgName VARCHAR(20)) RETURNS VARCHAR(4000) BEGIN DECLARE sPid VARCHAR(1000);
DECLARE sPidTemp VARCHAR(1000);
DECLARE pid VARCHAR(1000);
DECLARE count INT DEFAULT 0;
DECLARE allpid VARCHAR(4000);
SET sPidTemp = '';
SELECT GROUP_CONCAT(DISTINCT(CAST(id AS CHAR))) INTO sPid FROM vrv_org_tab WHERE org_name LIKE CONCAT('%',orgName,'%');
SET allpid = '';
WHILE count = 0
DO IF sPid IS NULL THEN SET allpid = '-1';
SET count = 1;
ELSE
SET pid = SUBSTRING_INDEX(sPid,',',1);
SET sPidTemp = CONCAT(sPidTemp,',',pid);
IF LENGTH(pid) = LENGTH(sPid) THEN
SET count = 1;
SET sPid = SUBSTRING(sPid FROM LENGTH(SUBSTRING_INDEX(sPid,',',1)) FOR LENGTH(sPid)+1);
ELSE
SET sPid = SUBSTRING(sPid FROM LENGTH(SUBSTRING_INDEX(sPid,',',1))+2 FOR LENGTH(sPid)+1);
END IF;
SELECT GROUP_CONCAT(CAST(id AS CHAR)) INTO sPidTemp FROM ( SELECT @r AS _id, (SELECT @r := org_parent_id FROM vrv_org_tab WHERE id = _id) AS parent_id, @l := @l + 1 AS lvl FROM (SELECT @r := pid, @l := 0) vars, vrv_org_tab h WHERE @r <> 0) T1 JOIN vrv_org_tab T2 ON T1._id = T2.id;
SET allpid = CONCAT_WS(',',pid,sPidTemp,allpid);
END IF;
END WHILE;
RETURN allpid;
END