【问题标题】:How to maintain the displayorder of oracle and postgreSQL?如何维护oracle和postgreSQL的显示顺序?
【发布时间】:2017-01-04 12:03:18
【问题描述】:

我希望 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;

预言机输出

对应的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 中生成相同的显示顺序?

【问题讨论】:

  • 对于 Postgres 部分,您只发布了插入,而不是用于获取数据的选择;在 Oracle 查询中,您没有 ORDER BY 子句,因此您并没有真正订购。请尝试澄清您的问题并发布您的完整代码。另外,请将数据以格式化文本的形式发布,not screenshots
  • 请注意“Oracle 输出”包含重复项。
  • 我已经编辑了问题
  • 如果没有order by,则不会定义行的顺序。您需要找到一种方法对行进行排序以获得有保证的排序顺序。

标签: postgresql oracle11g hierarchical-query


【解决方案1】:

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

像你这样的桌子:

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 获得相同的结果

【讨论】:

    猜你喜欢
    • 2010-12-11
    • 2012-04-12
    • 2016-08-29
    • 2014-08-10
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    相关资源
    最近更新 更多