The content of this page has been automatically translated by AI. If you encounter any problems while reading, you can view the corresponding content in Chinese.

with the use of Subquery and Recursion

Last updated: 2024-08-22 17:03:52

with the use of Subquery and Recursion

```sql
create table t_area(id int,pid int,area varchar);
insert into t_area values(1,null,'Guangdong Province');
insert into t_area values(2,1,'Shenzhen City');
insert into t_area values(3,1,'Dongguan City');
insert into t_area values(4,2,'Nanshan District');
insert into t_area values(5,2,'Futian District');
insert into t_area values(6,2,'Luohu District');
insert into t_area values(7,3,'Nancheng District');
insert into t_area values(8,3,'Dongcheng District');

with Subquery

Using with Subquery to select all rows with pid=1:
with t_gd_city AS (
select * from t_area where pid=1
)
select * FROM t_gd_city;
Query result:
id
pid
area
2
1
Shenzhen City
3
1
Dongguan City