简体   繁体   English

Oracle SQL按级别连接

[英]Oracle SQL connect by level

Can anyone explain the behavior of the below query:- 任何人都可以解释以下查询的行为: -

select level,t.*
from 
( select 'one','two'from dual
  union all
  select 'one','two'from dual
) t
connect by level<=2

There are 2 rows in the inner query. 内部查询中有2行。 I was expecting 4 rows of output, but i get 6 rows of output. 我期待4行输出,但我得到6行输出。 Why is it so and how does this work? 为什么会这样,这是如何工作的?

The query starts with one of your two rows and adds both rows, then it continues with the second row and adds both rows again. 查询从两行中的一行开始并添加两行,然后继续第二行并再次添加两行。

Change your query like this: 像这样更改您的查询:

select level,t.*
from 
( select 'one' from dual
  union all
  select 'two' from dual
) t
connect by level<=2;

This makes it easier to see what happens: 这样可以更容易地看到发生了什么:

1   one
2   one
2   two
1   two
2   one
2   two

Read this http://docs.oracle.com/cd/B19306_01/server.102/b14200/queries003.htm 阅读http://docs.oracle.com/cd/B19306_01/server.102/b14200/queries003.htm

When level <= 1 , you will get each of the records 1 time. level <= 1 ,您将获得每次记录1次。

When level <= 2 , then you will get each level 1 time (for level 1) + the number of records in the table (That means for this condition 2 records having level 1 + 2*2 records having level 2. This is the reason you are getting 6 records.) level <= 2 ,你将获得每个等级1次(对于等级1)+表中的记录数(这意味着对于这种情况,2个记录具有等级1 + 2 * 2记录具有等级2.这是你得到6条记录的原因。)

CONNECT BY LEVEL gives following number of rows x+x2+x3+x4+...x^n = Sx^n CONNECT BY LEVEL给出以下行数x + x2 + x3 + x4 + ... x ^ n = Sx ^ n

where n is number of LEVEL and x is number of rows in a table 其中n是LEVEL的数量,x是表格中的行数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM