mysql树级查询父节点,子节点集合

准备工作

1、表结构及数据

CREATE TABLE `tbl_group_ext` (
  `GRP_ID` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `PAR_ID` int(10) DEFAULT NULL COMMENT '父级节点',
  `GRP_NM` varchar(20) DEFAULT NULL COMMENT '节点名称',
  PRIMARY KEY (`GRP_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

insert  into `tbl_group_ext`(`GRP_ID`,`PAR_ID`,`GRP_NM`) values (1,0,'华为'),(2,0,'三星'),(3,2,'三星IT部'),(4,3,'三星IT部一组'),(5,3,'三星IT部二组'),(6,2,'三星财务部'),(7,6,'财务部一组'),(8,1,'华为IT部'),(9,8,'华为IT部一组'),(10,1,'华为财务部'),(11,10,'华为财务部一组');

2、利用函数方法

(1)、查询顶级节点函数

生成函数

DELIMITER $$
DROP FUNCTION IF EXISTS `getGrpTopId`$$
CREATE FUNCTION `getGrpTopId`(grpId BIGINT) RETURNS BIGINT(10)
BEGIN 
      DECLARE parId BIGINT DEFAULT 0;
      DECLARE topId BIGINT(10) DEFAULT grpId;
      WHILE grpId > 0 DO
        SET parId = (SELECT par_id FROM tbl_group_ext WHERE grp_id = grpId); 
        IF parId > 0 
        THEN
          SET topId = parId;   
          SET grpId = parId;  
        ELSE 
          SET grpId = -1;  
        END IF;  
      END WHILE;
      RETURN topId;
    END$$
DELIMITER ;

利用函数SQL查询
SELECT * FROM TBL_GROUP_EXT WHERE GRP_ID = getGrpTopId(param );
param:当前节点主键

(2)、查询所有父节点函数

生成函数

DELIMITER $$
DROP FUNCTION IF EXISTS `getGrpParIds`$$
CREATE  FUNCTION `getGrpParIds`(grpId BIGINT) RETURNS VARCHAR(100) CHARSET utf8
BEGIN 
      DECLARE parId BIGINT DEFAULT 0;
      DECLARE str VARCHAR(100) DEFAULT grpId; 
      WHILE grpId > 0 DO
        SET parId = (SELECT PAR_ID FROM TBL_GROUP_EXT WHERE GRP_ID = grpId); 
        IF parId > 0 
        THEN
          SET str = CONCAT(str, ',', parId);   
          SET grpId = parId;  
        ELSE 
          SET grpId = -1;  
        END IF;  
      END WHILE;
      RETURN str;
    END$$
DELIMITER ;

利用函数SQL查询
SELECT * FROM TBL_GROUP_EXT WHERE FIND_IN_SET(GRP_ID,getGrpParIds(param ))
param:当前节点主键

(3)、查询所有子节点函数

生成函数

DELIMITER $$
DROP FUNCTION IF EXISTS `getGrpChildIds`$$
CREATE FUNCTION `getGrpChildIds`(grpId BIGINT) RETURNS VARCHAR(100) CHARSET utf8
BEGIN   
DECLARE str VARCHAR(100);  
DECLARE cid VARCHAR(100);   
SET str = '';   
SET cid = grpId;   
WHILE cid IS NOT NULL DO   
    SET str = CONCAT(str, ',', cid);   
    SELECT GROUP_CONCAT(GRP_ID) INTO cid FROM TBL_GROUP_EXT WHERE FIND_IN_SET(PAR_ID, cid) > 0;   
END WHILE;
SET str =SUBSTRING(str,2);
RETURN str;   
END$$
DELIMITER ;

利用函数SQL查询
SELECT * FROM TBL_GROUP_EXT WHERE FIND_IN_SET(GRP_ID,getGrpChildIds(param ))
param:当前节点主键

3、mysql sql查询

(1)、sql 查询所有子节点(不包含自身)
SELECT * FROM
			 (
			    SELECT * FROM TBL_GROUP_EXT WHERE STS = '1'
			 ) T1,
			 (SELECT @PV := 主键ID ) T2
		WHERE ((FIND_IN_SET(PAR_ID,@PV) > 0
		AND @PV := CONCAT(@PV, ',', GRP_ID)))
(2)、sql查询所有父节点(包含自身)
SELECT T2.* FROM (
            SELECT
               @R AS _ID,
               (SELECT @R := PAR_ID FROM TBL_GROUP_EXT WHERE GRP_ID = _ID) AS PAR_ID,
               @L := @L + 1 AS LVL
            FROM
            (SELECT @R := 主键ID, @L := 0) VARS,
            TBL_GROUP_EXT H
            WHERE @R <> 0
        ) T1
        INNER JOIN TBL_GROUP_EXT T2 ON T1._ID = T2.GRP_ID
        ORDER BY T1.LVL DESC

你可能感兴趣的:(java,数据库)