家谱整站源码php_mysql家谱表查询某人所有后代
CREATE TABLE `people` (
`id` INT(11) NOT NULL,
`name` VARCHAR(50) NULL DEFAULT NULL,
`pid` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
CREATE DEFINER=`root`@`%` PROCEDURE `getChildren`(IN `parentId` INT)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT '獲取某人所有后代'
BEGIN
#存放結果的臨時表
DROP TABLE IF EXISTS children;
CREATE TEMPORARY TABLE children SELECT 0 pLevel,p.* FROM `people` p WHERE id=parentId;
#存放中間結果的臨時表
DROP TABLE IF EXISTS tem;
CREATE TEMPORARY TABLE tem SELECT id FROM `people` limit 0;
#逐級填充后代
SET @pLevel=1;
REPEAT
#清空上次數據
TRUNCATE TABLE tem;
#將當前level的后代id放入臨時表
INSERT INTO tem SELECT p.id FROM `people` P, children c
WHERE p.pid=c.id AND c.pLevel=(@pLevel-1);
#將當前level的后代數據塞入結果臨時表
INSERT INTO children SELECT @pLevel pLevel,p.* FROM `people` p, tem t
WHERE p.id=t.id;
SET @pLevel=@pLevel+1;
UNTIL NOT EXISTS (SELECT * FROM tem) OR @pLevel > 10
END REPEAT;
#調整表結構,刪除臨時列和不需要的數據
ALTER TABLE children DROP COLUMN pLevel;
DELETE FROM children WHERE id=parentId;
#返回結果
SELECT * from children;
END
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的家谱整站源码php_mysql家谱表查询某人所有后代的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python线程同步锁_[python]
- 下一篇: python安卓自动化实现方法_uiau