with 子查询

最近更新时间: 2024-06-12 15:06:00

#实验数据
create table t_area(id int,pid int,area varchar);
insert into t_area values(1,null,'广东省');
insert into t_area values(2,1,'深圳市');
insert into t_area values(3,1,'东莞市');
insert into t_area values(4,2,'南山区');
insert into t_area values(5,2,'福田区');
insert into t_area values(6,2,'罗湖区');
insert into t_area values(7,3,'南城区');
insert into t_area values(8,3,'东城区');
with 子查询
postgres=# with t_gd_city AS (
   select * from t_area where pid=1
)
select  * FROM t_gd_city;
 id | pid |  area  
----+-----+--------
  2 |   1 | 深圳市
  3 |   1 | 东莞市
(2 rows)