- 测试表结构
create table demo
(
id varchar(100) not null
primary key,
parentId varchar(100) not null
);
-- 测试数据
INSERT INTO demo (ID, parentID) VALUES ('1', '0');
INSERT INTO demo (ID, parentID) VALUES ('10', '9');
INSERT INTO demo (ID, parentID) VALUES ('11', '9');
INSERT INTO demo (ID, parentID) VALUES ('2', '1');
INSERT INTO demo (ID, parentID) VALUES ('3', '2');
INSERT INTO demo (ID, parentID) VALUES ('4', '3');
INSERT INTO demo (ID, parentID) VALUES ('5', '4');
INSERT INTO demo (ID, parentID) VALUES ('6', '5');
INSERT INTO demo (ID, parentID) VALUES ('7', '6');
INSERT INTO demo (ID, parentID) VALUES ('8', '1');
INSERT INTO demo (ID, parentID) VALUES ('9', '1');
- 向下递归(包括自己)
-- 向下递归 par即为递归查询出来的所有id
SELECT
@par AS par,
(SELECT @par := GROUP_CONCAT(id) FROM demo WHERE FIND_IN_SET(parentid, @par)) AS son
FROM demo_userm, (SELECT @par := '1') T
WHERE @par IS NOT NULL;
查询结果:
| par | son |
| 1 | 2,8,9 |
| 2,8,9 | 10,11,3 |
| 10 ,11,3 | 4 |
| 4 | 5 |
| 5 | 6 |
| 6 | 7 |
| 7 | NULL |
3.向下递归(不包括自己)
-- 向下递归 par即为递归查询出来的所有id
SELECT
@par AS par,
(SELECT @par := GROUP_CONCAT(id) FROM demo WHERE FIND_IN_SET(parentid, @par)) AS son
FROM demo_userm, (SELECT @par := '1') T
WHERE @par IS NOT NULL and @par != '1';
查询结果:
| par | son |
| 2,8,9 | 10,11,3 |
| 10 ,11,3 | 4 |
| 4 | 5 |
| 5 | 6 |
| 6 | 7 |
| 7 | NULL |
- 向下递归可能存在问题,修正优化:
-- 如果存在 par = 51016 并且 son = 51016 , 那么会出现问题,多出许多数据
SELECT
@par AS par,
(SELECT @par := GROUP_CONCAT(distinct ORG_REFNO) FROM gis_udp_bank WHERE FIND_IN_SET(PNBRN_ORG_REFNO, @par)) AS son
FROM gis_udp_bank, (SELECT @par := '51016') T
WHERE @par IS NOT NULL;
-- 优化后:后面家条件: !FIND_IN_SET(ORG_REFNO, @par)
SELECT
@par AS par,
(SELECT @par := GROUP_CONCAT(distinct ORG_REFNO) FROM gis_udp_bank WHERE FIND_IN_SET(PNBRN_ORG_REFNO, @par)
and !FIND_IN_SET(ORG_REFNO, @par)
) AS son
FROM gis_udp_bank, (SELECT @par := '51016') T
WHERE @par IS NOT NULL;
- 向上递归
SELECT
@par AS par,
(SELECT @par := GROUP_CONCAT(NAME) FROM demo_userm WHERE LOGONID = @par) AS son
FROM demo_userm, (SELECT @par := '10') T
WHERE @par IS NOT NULL;
par即为递归查询结果
查询结果:
| par | son |
| 10 | 9 |
| 9 | 1 |
| 1 | 0 |
| 0 | NULL|
所用函数介绍:
1、GROUP_CONCAT([distinct] colName)
一般用于取ID集合。
select * from demo_userm t where NAME = 1;
| LOGONID | NAME |
| 2 | 1 |
| 8 | 1 |
| 9 | 1 |
select GROUP_CONCAT(LOGONID) from demo_userm t where NAME = 1;
| GROUP\_CONCAT\(LOGONID\) |
| 2,8,9 |
-- 不去重取name集合
select GROUP_CONCAT(NAME) from demo_userm t where NAME in (1,2,3,4,5,6,7);
| GROUP\_CONCAT\(NAME\) |
| 1,2,3,4,5,6,1,1 |
-- 去重再取name集合
select GROUP_CONCAT(distinct NAME) from demo_userm t where NAME in (1,2,3,4,5,6,7);
| GROUP\_CONCAT\(distinct NAME\) |
| 1,2,3,4,5,6 |
2、FIND_IN_SET(colName, strs)
select * from demo_userm where FIND_IN_SET(LOGONID, ('1,2,3,4'));
| LOGONID | NAME |
| 1 | 0 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
select * from demo_userm where
FIND_IN_SET(LOGONID, (select GROUP_CONCAT(LOGONID) from demo_userm t where NAME = 1));
| LOGONID | NAME |
| 2 | 1 |
| 8 | 1 |
| 9 | 1 |
3、
-- @是用户变量,@@是系统变量。
-- 用户变量赋值有两种方式: 一种是直接用"=“号,另一种是用”:=“号。