Home > Database > Mysql Tutorial > body text

How does MySql use parent id to recursively query child nodes downwards?

王林
Release: 2023-05-30 16:03:28
forward
1102 people have browsed it

No need to write stored procedures, no need to build database functions, just a piece of sql can be implemented

SELECT
	ID.LEVEL,
	DATA.* 
FROM
	(
	SELECT
		@ids AS _ids,
		( SELECT @ids := GROUP_CONCAT( region_id ) FROM region WHERE FIND_IN_SET(parent_id, @ids ) ) AS cids,
		@l := @l + 1 AS LEVEL 
	FROM
		region,
		( SELECT @ids := 3, @l := 0 ) b 
	WHERE
		@ids IS NOT NULL 
	) ID,
	region DATA 
WHERE
	FIND_IN_SET( DATA.region_id, ID._ids ) 
ORDER BY
	LEVEL
Copy after login

How does MySql use parent id to recursively query child nodes downwards?

Test

--创建测试环境
create table t_test(
	id int PRIMARY key,
	parent_id int,
	name varchar(200)
)

insert t_test VALUES(1,null,"中国");

insert t_test VALUES(2,1,"华北");

insert t_test VALUES(3,2,"山西省");
insert t_test VALUES(4,2,"北京");

insert t_test VALUES(5,3,"临汾市");
insert t_test VALUES(6,4,"北京市");


insert t_test VALUES(7,5,"尧都区");
insert t_test VALUES(8,6,"朝阳区");

insert t_test VALUES(9,7,"解放西路");
insert t_test VALUES(10,8,"朝阳北路");


SELECT * FROM t_test;
Copy after login

Test data display

How does MySql use parent id to recursively query child nodes downwards?

Query id=1, query what places are below China

SELECT
	ID.LEVEL,
	DATA.* 
FROM
	(
	SELECT
		@ids AS _ids,
		( SELECT @ids := GROUP_CONCAT( id ) FROM t_test WHERE FIND_IN_SET(parent_id, @ids ) ) AS cids,
		@l := @l + 1 AS LEVEL 
	FROM
		t_test,
		( SELECT @ids := 1, @l := 0 ) b 
	WHERE
		@ids IS NOT NULL 
	) ID,
	t_test DATA 
WHERE
	FIND_IN_SET( DATA.id, ID._ids ) 
ORDER BY
	LEVEL
Copy after login

How does MySql use parent id to recursively query child nodes downwards?

id=3, check what places are under Shanxi

SELECT
	ID.LEVEL,
	DATA.* 
FROM
	(
	SELECT
		@ids AS _ids,
		( SELECT @ids := GROUP_CONCAT( id ) FROM t_test WHERE FIND_IN_SET(parent_id, @ids ) ) AS cids,
		@l := @l + 1 AS LEVEL 
	FROM
		t_test,
		( SELECT @ids := 3, @l := 0 ) b 
	WHERE
		@ids IS NOT NULL 
	) ID,
	t_test DATA 
WHERE
	FIND_IN_SET( DATA.id, ID._ids ) 
ORDER BY
	LEVEL
Copy after login

How does MySql use parent id to recursively query child nodes downwards?

id=4, check what places are under Beijing

How does MySql use parent id to recursively query child nodes downwards?

Finally, query down from the North China region with id=2

How does MySql use parent id to recursively query child nodes downwards?

The above is the detailed content of How does MySql use parent id to recursively query child nodes downwards?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!