如何维护oracle和postgreSQL的显示顺序?

How to maintain the displayorder of oracle and postgreSQL?

我希望 postgreSQL 中的显示顺序与 oracle 输出中生成的顺序相同。 我尝试使用以下 oracle 查询和如下所示的输出

create table student(
std_id int, std_roll int
);


SELECT std_id, std_roll
FROM   student
CONNECT BY PRIOR std_id = std_roll;

oracle 输出

对应的postgreSQL查询和输出

create table student(
std_id int, std_roll int
);


INSERT into student values( 1, 0 );
INSERT into student values( 2, 1 );
INSERT into student values( 3, 1 );
INSERT into student values( 4, 2 );
INSERT into student values( 5, 2 );
INSERT into student values( 6, 3 );
INSERT into student values( 7, 4 );

 WITH RECURSIVE q AS (
 SELECT po.std_id,po.std_roll
 FROM student po
 UNION ALL
 SELECT po.std_id,po.std_roll
 FROM student po
 JOIN q ON q.std_id=po.std_roll
 )
 SELECT * FROM q;

postgreSQL 输出

是否可以在 postgreSQL 中保持与在 oracle 中生成的相同的显示顺序?

根据您发布的数据,您似乎需要在 Oracle 中 ORDER SIBLINGS

和你一样的table:

create table student(
std_id int, std_roll int
);
INSERT into student values( 1, 0 );
INSERT into student values( 2, 1 );
INSERT into student values( 3, 1 );
INSERT into student values( 4, 2 );
INSERT into student values( 5, 2 );
INSERT into student values( 6, 3 );
INSERT into student values( 7, 4 );

这给出了您需要的输出:

SELECT std_id, std_roll, level
FROM   student
CONNECT BY PRIOR std_id = std_roll
order siblings by std_id

在 Postgres 中,您应该能够通过使用 this answer

获得相同的结果