Created
July 20, 2017 04:00
-
-
Save keehyun2/2e23308282a796a2ecbd5946e1beb60a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEGIN | |
DECLARE _id INT; | |
DECLARE _parent INT; | |
DECLARE CONTINUE HANDLER FOR NOT FOUND SET @id = NULL; | |
SET _parent = @id; | |
SET _id = -1; | |
SET @loop_cnt = 0; | |
IF @id IS NULL THEN | |
RETURN NULL; | |
END IF; | |
loop | |
SET @loop_cnt = @loop_cnt + 1; | |
SELECT MIN(id) | |
INTO @id | |
FROM test_k | |
WHERE parent = _parent AND id > _id; | |
IF @id IS NOT NULL OR _parent = @start_with THEN | |
SET @level = @level + 1; | |
-------------------------------------------------------------------------------------------------- | |
insert into temp_log(_id, _parent, min) select _id , _parent , (SELECT MIN(id) | |
FROM test_k | |
WHERE parent = _parent AND id > _id) ; | |
-------------------------------------------------------------------------------------------------- | |
RETURN @id; | |
END IF; | |
SET @level := @level - 1; | |
SELECT id, parent | |
INTO _id, _parent | |
FROM test_k | |
WHERE id = _parent; | |
end loop; | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment