<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 递归查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)

                  <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>