递归取追顶层

CREATE DEFINER=`root`@`%` FUNCTION `GET_FIRST_PARENT_ID`(id VARCHAR(50)) RETURNS varchar(10000) CHARSET utf8mb4
    SQL SECURITY INVOKER
BEGIN
  DECLARE r VARCHAR(10000) default '';
  DECLARE tmp VARCHAR(10000) default '';    
    
    WHILE tmp != '0' DO
        
        set r = (SELECT region_frame_parent_id FROM hz_region_frame where region_frame_id = id);
        
        IF r != '0' THEN
            set id = r;
            set tmp = r;
        ELSE 
          set tmp = r;
            SET r = id;
        END IF;
        
    END WHILE;
    
RETURN r;
END

你可能感兴趣的:(递归取追顶层)