Detailed rownum in oracle

1. Overview
rownum is falsifying Oracle introduced.
The false statement does not physically exist, but only when the query constructed. Pseudo columns are typically free distribution, the user can not perform modification and other operations.

2. Features
About rownum has the following main features:

1) rownum not belong to any table.
2) the premise of the existence of rownum, first, the results table.
3) rownum always starts at 1.
4) rownum and generally <(<=) used together.
5) Use rownum rownum paging queries need to be translated into real column, and for the rownum query.
2. The principle mechanism
rownum usage may seem strange, in fact, if understand the principles of the mechanism will be very simple.
First of all we have to do a simple experiment: Remove all fields from the dual table, and remove the rownum.
If we adopt the following wording: t.rownum

This run will be reported 01,747 wrong:


Because in fact, dual rownum table does not exist in this field, so we can not use t.rownum format.
The correct wording should be:

So, rownum is a false statement, it does not belong to any table.

So how come this false statement. We do a simple experiment, easy to understand:
the following, we have a simple table: test_ljb, a total of ten records.
We add rownum.

As a result, is well understood, ten selected record, from 1 to 10 rownum


We add a salary of filter criteria:

The results are as follows: selecting the three records, rownum from 1 to 3


Note that, rownum second result table corresponding to the first employee and does not correspond.
Such as: In the first table is rownum 1, when the corresponding Arvin, while the second corresponds to Oracle.

: For the following reasons
plus up a row because rownum is added to the result set of a pseudo-column, which is to be found in the result set later.
Simply put, rownum is the serial number of the qualifying results. It always start from a waiting, it is not possible to skip a selected result, and the other values greater than 1.
Or, rownum is a dynamic, real-time according to the new changes in the result set.

For example, the following statement:

select t *, rownum from test_ljb t where rownum> 1; -. 2,3 or any other value greater than greater than 1, the same results.

We found no records that meet the criteria.

According to the principle, rownum begins with 1 row of the result set. What results are set above statement is it?

In fact, when performing finish from test_ljb, we can regard him as a results table, rownum is from 1-10.
Then, the focus, when we filter them, rownum> 1, the first record does not satisfy, removed. This time, the new result set is generated, the original second recording has become the first, the corresponding rownum becomes 1-9.
Again comparing the original first, and now the second record, he also rownum 1, not satisfied, rownum is 1-8.
Records and so on, running water, iron rownum from the beginning. So, until rownum of 1, was not satisfied. So in the end no records are filtered out, nor the rownum.

So, we write this type of statement:

All are no results.

But interestingly, elected the first ten, you can have a lot of writing:

3. Use
that some students just whispered, and I want to be greater than the query how do ah, paging query how do ah, people Mysql and Hive a limit a, b directly to the bin, how do you Oracle.
In fact, the method is still there, but also with rownum, but first put the goods into a solid column. Plus sub-query on it.
The old routine, simple experiments go wave.

Greater than 3.1 query
or test_ljb table, chose> 5 lines.

3.2.1 Simple paging query

3.3.2 Sort paging query
sort paging query in trouble, first of all to sort, filter and then sorted on the basis of then.
Of course, this is actually the most common project.
Select the highest salary 4,5,6 months.

 

It should be noted that: rownum only for the new result set dynamic labels, and sort and will not generate a new set of results, if only execution

The results are as follows:

Source: CSDN
Original: https: //blog.csdn.net/qq_36743482/article/details/78919904
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/kavins/p/12462996.html