mysql樹狀查詢(轉)
轉自https://explainextended.com/2009/03/17/hierarchical-queries-in-mysql/
--建立表
drop table if exists t_hierarchy;
CREATE TABLE t_hierarchy (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
parent int(10) unsigned NOT NULL,
PRIMARY KEY (id),
KEY ix_hierarchy_parent (parent, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--建立儲存過程插入資料
DROP PROCEDURE if exists prc_fill_hierarchy;
DELIMITER $$
CREATE PROCEDURE prc_fill_hierarchy (level INT, fill INT)
BEGIN
DECLARE _level INT;
DECLARE _fill INT;
INSERT
INTO t_hierarchy (id, parent)
VALUES (1, 0);
SET _fill = 0;
WHILE _fill < fill DO
INSERT
INTO t_hierarchy (parent)
VALUES (1);
SET _fill = _fill + 1;
END WHILE;
SET _fill = 1;
SET _level = 0;
WHILE _level < level DO
INSERT
INTO t_hierarchy (parent)
SELECT hn.id
FROM t_hierarchy ho, t_hierarchy hn
WHERE ho.parent = 1
AND hn.id > _fill;
SET _level = _level + 1;
SET _fill = _fill + POWER(fill, _level);
END WHILE;
END
$$
DELIMITER ;
--插入資料
START TRANSACTION;
CALL prc_fill_hierarchy(3, 2);
COMMIT;
--建立生成樹的函式
DELIMITER $$
CREATE FUNCTION hierarchy_connect_by_parent_eq_prior_id(value INT) RETURNS INT
NOT DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE _id INT;
DECLARE _parent INT;
DECLARE _next INT;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET @id = NULL;
SET _parent = @id;
SET _id = -1;
IF @id IS NULL THEN
RETURN NULL;
END IF;
LOOP
SELECT MIN(id)
INTO @id
FROM t_hierarchy
WHERE parent = _parent
AND id > _id;
IF @id IS NOT NULL OR _parent = @start_with THEN
SET @level = @level + 1;
RETURN @id;
END IF;
SET @level := @level - 1;
SELECT id, parent
INTO _id, _parent
FROM t_hierarchy
WHERE id = _parent;
END LOOP;
END
$$
DELIMITER ;
--生成tree的查詢
SELECT CONCAT(REPEAT('--->', level - 1), CAST(hi.id AS CHAR)) AS treeitem, parent, level
FROM (
SELECT hierarchy_connect_by_parent_eq_prior_id(id) AS id, @level AS level
FROM (
SELECT @start_with := 0,
@id := @start_with,
@level := 0
) vars, t_hierarchy
WHERE @id IS NOT NULL
) ho
JOIN t_hierarchy hi
ON hi.id = ho.id
--建立表
drop table if exists t_hierarchy;
CREATE TABLE t_hierarchy (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
parent int(10) unsigned NOT NULL,
PRIMARY KEY (id),
KEY ix_hierarchy_parent (parent, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--建立儲存過程插入資料
DROP PROCEDURE if exists prc_fill_hierarchy;
DELIMITER $$
CREATE PROCEDURE prc_fill_hierarchy (level INT, fill INT)
BEGIN
DECLARE _level INT;
DECLARE _fill INT;
INSERT
INTO t_hierarchy (id, parent)
VALUES (1, 0);
SET _fill = 0;
WHILE _fill < fill DO
INSERT
INTO t_hierarchy (parent)
VALUES (1);
SET _fill = _fill + 1;
END WHILE;
SET _fill = 1;
SET _level = 0;
WHILE _level < level DO
INSERT
INTO t_hierarchy (parent)
SELECT hn.id
FROM t_hierarchy ho, t_hierarchy hn
WHERE ho.parent = 1
AND hn.id > _fill;
SET _level = _level + 1;
SET _fill = _fill + POWER(fill, _level);
END WHILE;
END
$$
DELIMITER ;
--插入資料
START TRANSACTION;
CALL prc_fill_hierarchy(3, 2);
COMMIT;
--建立生成樹的函式
DELIMITER $$
CREATE FUNCTION hierarchy_connect_by_parent_eq_prior_id(value INT) RETURNS INT
NOT DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE _id INT;
DECLARE _parent INT;
DECLARE _next INT;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET @id = NULL;
SET _parent = @id;
SET _id = -1;
IF @id IS NULL THEN
RETURN NULL;
END IF;
LOOP
SELECT MIN(id)
INTO @id
FROM t_hierarchy
WHERE parent = _parent
AND id > _id;
IF @id IS NOT NULL OR _parent = @start_with THEN
SET @level = @level + 1;
RETURN @id;
END IF;
SET @level := @level - 1;
SELECT id, parent
INTO _id, _parent
FROM t_hierarchy
WHERE id = _parent;
END LOOP;
END
$$
DELIMITER ;
--生成tree的查詢
SELECT CONCAT(REPEAT('--->', level - 1), CAST(hi.id AS CHAR)) AS treeitem, parent, level
FROM (
SELECT hierarchy_connect_by_parent_eq_prior_id(id) AS id, @level AS level
FROM (
SELECT @start_with := 0,
@id := @start_with,
@level := 0
) vars, t_hierarchy
WHERE @id IS NOT NULL
) ho
JOIN t_hierarchy hi
ON hi.id = ho.id
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10972173/viewspace-2143075/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL實現樹狀所有子節點查詢的方法MySql
- MySQL——優化巢狀查詢和分頁查詢MySql優化巢狀
- 帶你玩玩轉 MySQL 查詢MySql
- Mysql 巢狀查詢100例子MySql巢狀
- 樹狀陣列模板題 & (樹狀陣列 1:單點修改,區間查詢)陣列
- MySQL字串轉時間戳查詢MySql字串時間戳
- MySQL執行狀況查詢方式介紹MySql
- 樹狀資料結構儲存方式——查詢篇資料結構
- [玩轉MySQL之六]MySQL查詢優化器MySql優化
- 平衡查詢樹
- 多路查詢樹
- Mysql中的巢狀子查詢問題QSBSMySql巢狀
- HDU 1556【區間更新+單點查詢 樹狀陣列】陣列
- 樹狀陣列的區間查詢與區間修改陣列
- bzoj3110: [Zjoi2013]K大數查詢(主席樹+樹狀陣列)陣列
- MySQL查詢MySql
- MySQL函式查詢目錄樹問題記錄MySql函式
- 聊聊mysql的樹形結構儲存及查詢MySql
- 查詢最佳化——查詢樹結構
- MySQL like查詢字元轉義遇到的坑MySql字元
- Excel資料庫轉MySQL,實現查詢Excel資料庫MySql
- Mysql慢查詢日誌檔案轉ExcelMySqlExcel
- 巢狀子查詢巢狀
- oracle樹形查詢Oracle
- POJ 3468 【區間修改+區間查詢 樹狀陣列 | 線段樹 | 分塊】陣列
- 隨筆:MySQL 查詢事務狀態欄位說明MySql
- oracle之樹狀結構的儲存與展示(遞迴查詢)Oracle遞迴
- MySQL - 資料查詢 - 簡單查詢MySql
- mysql查詢模型MySql模型
- MYsql 子查詢MySql
- 【MySQL】多表查詢MySql
- mysql多表查詢MySql
- MySQL子查詢MySql
- MySQL慢查詢MySql
- MySQL 多表查詢MySql
- mysql 模糊查詢MySql
- MySQL 慢查詢MySql
- 二叉查詢樹
- mysql-分組查詢-子查詢-連線查詢-組合查詢MySql