oracle 按 level<=4 从双连接中选择 level<=4 内部如何工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42184715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:23:18  来源:igfitidea点击:

select level from dual connect by level<=4 how it works internally

oracle

提问by Ram

I know level usage along with hierarchical queries but I am not able to understand how to analyze this:

我知道级别用法和分层查询,但我无法理解如何分析这个:

(select level from dual connect by level<=4) 

query works internally and how it is generating numbers.

查询在内部工作以及它是如何生成数字的。

For generating numbers I had another way that is:

为了生成数字,我有另一种方法:

select r from (select rownum r from all_objects) e where r<=10 

it will generate the first 10 numbers. But I am not able to understand how level is working internally.

它将生成前 10 个数字。但我无法理解 level 在内部是如何工作的。

Please explain why!

请解释原因!

回答by gregory

This builds a hierarchical query. The connect bydefines how to walk from the parent node to its children node and their children's children node. In this case, your definition of connection is only when level >= 4. The trick here is that levelis pseudo column, like rownum, so the condition is satisfied only by the less-than equal number supplied (in this case 4). You can also do this with rownum:

这将构建一个分层查询。在connect by定义了如何从父节点到其子节点和他们的孩子的孩子节点走。在这种情况下,您对连接的定义仅在level >= 4. 这里的技巧level是伪列,如rownum,因此条件仅由提供的小于等于的数字(在本例中为 4)满足。您也可以使用 rownum 执行此操作:

select level from dual connect by rownum <= 4; 

Levelis used to count the depth of the connections, so a parent would be 1, a child 2, a child of the child 3, etc. So think of it a rownum for hierarchical queries.

Level用于计算连接的深度,因此父级为 1,子级为 2,子级为 3 的子级,等等。因此将其视为用于分层查询的 rownum。