<i id='UPelo'><tr id='UPelo'><dt id='UPelo'><q id='UPelo'><span id='UPelo'><b id='UPelo'><form id='UPelo'><ins id='UPelo'></ins><ul id='UPelo'></ul><sub id='UPelo'></sub></form><legend id='UPelo'></legend><bdo id='UPelo'><pre id='UPelo'><center id='UPelo'></center></pre></bdo></b><th id='UPelo'></th></span></q></dt></tr></i><div id='UPelo'><tfoot id='UPelo'></tfoot><dl id='UPelo'><fieldset id='UPelo'></fieldset></dl></div>
  • <legend id='UPelo'><style id='UPelo'><dir id='UPelo'><q id='UPelo'></q></dir></style></legend>

      • <bdo id='UPelo'></bdo><ul id='UPelo'></ul>

      <small id='UPelo'></small><noframes id='UPelo'>

    1. <tfoot id='UPelo'></tfoot>

        自引用表 (Oracle) 上的 SQL 递归查询

        SQL recursive query on self referencing table (Oracle)(自引用表 (Oracle) 上的 SQL 递归查询)
        <legend id='imbfE'><style id='imbfE'><dir id='imbfE'><q id='imbfE'></q></dir></style></legend>
        • <small id='imbfE'></small><noframes id='imbfE'>

            <tfoot id='imbfE'></tfoot>
            <i id='imbfE'><tr id='imbfE'><dt id='imbfE'><q id='imbfE'><span id='imbfE'><b id='imbfE'><form id='imbfE'><ins id='imbfE'></ins><ul id='imbfE'></ul><sub id='imbfE'></sub></form><legend id='imbfE'></legend><bdo id='imbfE'><pre id='imbfE'><center id='imbfE'></center></pre></bdo></b><th id='imbfE'></th></span></q></dt></tr></i><div id='imbfE'><tfoot id='imbfE'></tfoot><dl id='imbfE'><fieldset id='imbfE'></fieldset></dl></div>

              <bdo id='imbfE'></bdo><ul id='imbfE'></ul>

                    <tbody id='imbfE'></tbody>
                  本文介绍了自引用表 (Oracle) 上的 SQL 递归查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有这个样本数据:

                  Lets assume I have this sample data:

                  | Name     | ID | PARENT_ID |
                  -----------------------------
                  | a1       | 1  | null      |
                  | b2       | 2  | null      |
                  | c3       | 3  | null      |
                  | a1.d4    | 4  | 1         |
                  | a1.e5    | 5  | 1         |
                  | a1.d4.f6 | 6  | 4         |
                  | a1.d4.g7 | 7  | 4         |
                  | a1.e5.h8 | 8  | 5         |
                  | a2.i9    | 9  | 2         |
                  | a2.i9.j10| 10 | 9         |
                  

                  我想选择从 accountId = 1 开始的所有记录,所以预期的结果是:

                  I would like to select all records start from accountId = 1, so the expected result would be:

                  | Name     | ID | PARENT_NAME | PARENT_ID | 
                  -------------------------------------------
                  | a1       | 1  | null        | null      |
                  | a1.d4    | 4  | a1          | 1         |
                  | a1.e5    | 5  | a1          | 1         |
                  | a1.d4.f6 | 6  | a1.d4       | 4         |
                  | a1.d4.g7 | 7  | a1.d4       | 4         |
                  | a1.e5.h8 | 8  | a1.e5       | 5         |
                  

                  我目前能够进行递归选择,但是我无法从父引用访问数据,因此我无法返回 parent_name.我使用的代码是(适用于简单的例子):

                  I am currently able to make the recursive select, but then I can't access the data from the parent reference, hence I can't return parent_name. The code I'm using is (adapted to the simplistic example):

                  SELECT id, parent_id, name
                  FROM tbl 
                    START WITH id = 1 
                    CONNECT BY PRIOR id = parent_id
                  

                  我应该使用什么 SQL 来进行上述检索?

                  What SQL should I be using to the mentioned above retrieval?

                  未来搜索者的额外关键词:SQL 选择由同一个表中的父键表示的分层数据

                  推荐答案

                  使用:

                      SELECT t1.id, 
                             t1.parent_id, 
                             t1.name,
                             t2.name AS parent_name,
                             t2.id AS parent_id
                        FROM tbl t1
                   LEFT JOIN tbl t2 ON t2.id = t1.parent_id
                  START WITH t1.id = 1 
                  CONNECT BY PRIOR t1.id = t1.parent_id
                  

                  这篇关于自引用表 (Oracle) 上的 SQL 递归查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Oracle PL/SQL - Raise User-Defined Exception With Custom SQLERRM(Oracle PL/SQL - 使用自定义 SQLERRM 引发用户定义的异常)
                  Oracle: is there a tool to trace queries, like Profiler for sql server?(Oracle:是否有跟踪查询的工具,例如用于 sql server 的 Profiler?)
                  SELECT INTO using Oracle(使用 Oracle SELECT INTO)
                  How to handle Day Light Saving in Oracle database(如何在 Oracle 数据库中处理夏令时)
                  PL/SQL - Use quot;Listquot; Variable in Where In Clause(PL/SQL - 使用“列表Where In 子句中的变量)
                  Oracle: Import CSV file(Oracle:导入 CSV 文件)

                  <small id='LFriP'></small><noframes id='LFriP'>

                    <bdo id='LFriP'></bdo><ul id='LFriP'></ul>
                        <i id='LFriP'><tr id='LFriP'><dt id='LFriP'><q id='LFriP'><span id='LFriP'><b id='LFriP'><form id='LFriP'><ins id='LFriP'></ins><ul id='LFriP'></ul><sub id='LFriP'></sub></form><legend id='LFriP'></legend><bdo id='LFriP'><pre id='LFriP'><center id='LFriP'></center></pre></bdo></b><th id='LFriP'></th></span></q></dt></tr></i><div id='LFriP'><tfoot id='LFriP'></tfoot><dl id='LFriP'><fieldset id='LFriP'></fieldset></dl></div>
                          <tfoot id='LFriP'></tfoot>
                              <tbody id='LFriP'></tbody>
                            <legend id='LFriP'><style id='LFriP'><dir id='LFriP'><q id='LFriP'></q></dir></style></legend>