CREATE DATABASE circuit_diagram; CREATE TABLE circuit_diagram_overview ( id bigserial not null, name text not null, created_at timestamp with time zone default CURRENT_TIMESTAMP not null, updated_at timestamp with time zone default CURRENT_TIMESTAMP not null ); CREATE TABLE circuit_diagram_1 ( id bigserial not null, name text not null, created_at timestamp with time zone default CURRENT_TIMESTAMP not null, updated_at timestamp with time zone default CURRENT_TIMESTAMP not null, parent_id bigint default 0 not null, other_params text not null ); INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(1,'母线','2024-11-07 09:37:00','2024-11-07 09:37:00',0,'voltage:35,status:0'); INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(2,'电流互感器','2024-11-07 09:37:00','2024-11-07 09:37:00',1,'voltage:35,status:0'); INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(3,'过继电流器','2024-11-07 09:37:00','2024-11-07 09:37:00',1,'voltage:35,status:0'); INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(4,'断路器','2024-11-07 09:37:00','2024-11-07 09:37:00',2,'voltage:35,status:0'); INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(5,'变压器','2024-11-07 09:37:00','2024-11-07 09:37:00',4,'voltage:35,status:0'); INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(6,'线缆','2024-11-07 09:37:00','2024-11-07 09:37:00',5,'voltage:35,status:0'); INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(7,'低压母线','2024-11-07 09:37:00','2024-11-07 09:37:00',6,'voltage:35,status:0'); INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(8,' 电动机 1','2024-11-07 09:37:00','2024-11-07 09:37:00',7,'voltage:35,status:0'); INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(9,' 电动机 2','2024-11-07 09:37:00','2024-11-07 09:37:00',7,'voltage:35,status:0'); SELECT id,parent_id,name,other_params FROM circuit_diagram_1 WHERE id = 1 WITH RECURSIVE recursive_tree as ( SELECT id,parent_id,name,other_params FROM circuit_diagram_1 WHERE id = 1 UNION ALL SELECT ctd.id,ctd.parent_id,ctd.name,ctd.other_params FROM circuit_diagram_1 ctd JOIN recursive_tree rt ON ctd.parent_id = rt.id ) SELECT * FROM recursive_tree;