如何解决SQL查询在一行中选择父级和子级名称
解决方法
您在这里:
with recursive
n (root_id,id,title,parent) as (
select id as root_id,name,parent from t
union
select n.root_id,t.id,t.name || '/' || n.title,t.parent
from n
join t on t.id = n.parent
)
select title as name from n where parent is null order by root_id
结果:
NAME
----------
A
A/A1
B
B/B1
C
C/C1
A/A1/A2
A/A1/A3
A/A1/A3/A4
B/B1/B3
作为参考,这是我用来测试的数据脚本:
create table t (id int,name varchar(10),parent int);
insert into t (id,parent) values
(0,'A',null),(1,'A1',0),(2,'B',(3,'B1',2),(4,'C',(5,'C1',4),(6,'A2',1),(7,'A3',(8,'A4',7),(9,'B3',3);
,
您可以使用递归查询:
with recursive cte (id,parent,path,lvl) as
select id,0 from mytable
union all
select c.id,c.name,t.parent,concat(t.name,'/',c.path),c.lvl + 1
from cte c
inner join mytable t on t.id = c.parent_id
)
select *
from cte c
where lvl = (select max(c1.lvl) from cte c1 where c1.id = c.id)
查询选择整个表,然后爬到每一行的层次结构树。这种方法的好处是可以正确处理“断”树(例如,像一个有无效父母的孩子)。
,基于接受的答案的另一种更简单的查询。我们仅从根(具有空父级的行)开始,每次执行联合时,仅添加几行。如答案所示,这种查询不会选择具有无效父ID的子行:
with recursive n (root_id,parent from t where parent is null
union
select n.root_id,n.title || '/' || t.name,t.parent
from t
join n on n.id = t.parent
)
select * from n
对于这种类型的表,必须添加设计中隐含的引用约束。当表变大时,这将加快查询速度,并避免损坏父ID。
alter table t add constraint pk primary key(id)
alter table t add constraint fk foreign key (parent) references t(id)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。